How can I vectorize this code?

1 view (last 30 days)
Luis
Luis on 18 Dec 2012
Hello everyone, I am trying to vectorize the following loop but I don´t quite get how to do it:
c=5;
for i=1:Machines;
for j=1:degradationbyM(i);
distLoadDec(i,j)=initialDataV(c,1);
paramDistLoadDec(i,j,1)=initialDataV(c,2);
paramDistLoadDec(i,j,2)=initialDataV(c,3);
paramDistLoadDec(i,j,3)=initialDataV(c,4);
c=c+1;
end
end
Thank you.
  2 Comments
John Petersen
John Petersen on 18 Dec 2012
Do these loops do exactly what you want? I see that the matrices in your for loop will not completely be filled. Is this your intention to have a variable index degredationbyM? You haven't shown any initialization for these matrices, or defined contador.
Daniel Shub
Daniel Shub on 19 Dec 2012
Why do you want to vectorize the code?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Dec 2012
c=5;
for i=1:Machines;
j = 1:degradationbyM(i); %not a "for" loop!
distLoadDec(i, j) = initialDataV(c+j-1, 1);
paramDistLoadDec(i, j, 1:3) = initialDataV(c+j-1, 2:4);
c = c + degradationbyM(i);
end

More 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!