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

답변 (1개)

James Tursa
James Tursa 2015년 6월 10일
편집: James Tursa 2015년 6월 10일
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.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 6월 10일

편집:

2015년 6월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by