Using matrix operation on a function instead of for loop
Ältere Kommentare anzeigen
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
Antworten (1)
James Tursa
am 10 Jun. 2015
Bearbeitet: James Tursa
am 10 Jun. 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.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!