Borrar filtros
Borrar filtros

Efficient implementation of a functions with vector arguments

1 visualización (últimos 30 días)
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 comentario
Sean de Wolski
Sean de Wolski el 3 de Abr. de 2013
What does your real vector look like and what would you expect for the output?

Iniciar sesión para comentar.

Respuesta aceptada

Doug Hull
Doug Hull el 3 de Abr. de 2013
Editada: Doug Hull el 3 de Abr. de 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

Más respuestas (0)

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by