Using matrix operation on a function instead of for loop

Hi, My use case scenario is as follows:
for i=1:1:loop_count;
out[i] = my_func(config_struct, i);
end
"config_struct" is a structure of function coefficients.
Is there any way to get rid of "for" loop as this is very slow?
Solution with an example would be very helpful.
Thanks, Atul

Respuestas (1)

James Tursa
James Tursa el 10 de Jun. de 2015
Editada: James Tursa el 10 de Jun. de 2015
The only way to get rid of the for loop is to rewrite the function my_func to operate in a vectorized manner (assuming it doesn't already). This in and of itself will not necessarily give you much speed increase ... it will depend on what my_func does.
Have you pre-allocated out? I.e., have you put this line before the loop?
out = zeros(1,loop_count); % Assuming my_func returns a double scalar
You should also look into how my_func is doing its calculations ... maybe there is a way to speed it up.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Jun. de 2015

Editada:

el 10 de Jun. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by