Converting nested for loops to vectors

2 views (last 30 days)
Daniel
Daniel on 19 Jan 2016
Edited: Stephen23 on 19 Jan 2016
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 Comments
Stephen23
Stephen23 on 19 Jan 2016
Edited: Stephen23 on 19 Jan 2016
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 on 19 Jan 2016
nevermind, I think I figured out my issue

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements 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!