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
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 についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!