Converting nested for loops to vectors

4 次查看(过去 30 天)
All,
I'm trying to convert the following code from nested for loops in order to improve efficiency.
c = 1;
output = zeros(n*m*z,1)
for i =1:n
for j = 1:m
for k = 1:z
output(c,:) = function(i, j, k);
c = c + 1;
end
end
end
is this something that can be vectorized or do I have to try to use the parallel computing toolbox and make use of the parfor looping?
thanks,
dan
  2 个评论
Stephen23
Stephen23 2016-1-19
编辑:Stephen23 2016-1-19
Vectorization does not mean replacing my slow loops with some other code, it means writing functions that operate on entire arrays at once.
This means it is the function that is important, not the loops. However you do not tell us anything about the function, yet it is the only important thing to consider. If you give us details about the function then we can give advice about vectorization.
In general vectorization is more efficient than jumping into using parallel computing.
Daniel
Daniel 2016-1-19
nevermind, I think I figured out my issue

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by