Using matrix operation on a function instead of for loop

2 views (last 30 days)
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

Answers (1)

James Tursa
James Tursa on 10 Jun 2015
Edited: James Tursa on 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.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!