Filter löschen
Filter löschen

Efficient implementation of a functions with vector arguments

1 Ansicht (letzte 30 Tage)
I am an inexperienced MATLAB user. I need to define functions with an efficient implementation because they will be called many times. The function arguments are 3-D vectors, and the functions need access to individual elements of the vectors. The following is a toy example.
function y = foo( vector )
y = vector(1)*cos(vector(2))/(1+vector(3));
end
--------------------------
Then I could do
v = [2,pi/3,2];
foo(v)
ans = 0.3333
Is there a more efficient way to implement the above function?
Thanks, Ted Ersek
  1 Kommentar
Sean de Wolski
Sean de Wolski am 3 Apr. 2013
What does your real vector look like and what would you expect for the output?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Doug Hull
Doug Hull am 3 Apr. 2013
Bearbeitet: Doug Hull am 3 Apr. 2013
Do not worry about efficiency until you know it is a problem. I think you might be surprised where the bottlenecks in your code are. First write clear, correct, maintainable code. When it is found to be slow, then you can optimize for speed. You will likely find that the maintainable code is faster than you need.
I would be VERY surprised if this code was the slowdown in any realistic code.
Doug

Weitere Antworten (0)

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by