Vectorizating a for loop

3 views (last 30 days)
Mariam Salem
Mariam Salem on 4 Sep 2017
Commented: Walter Roberson on 5 Sep 2017
Dear all,
I have this for loop that I want to vectorize, the loop is shown below:
k=1;j=1;shift=0; ngaps= 42;
for i=1:ngaps+1
RC(k:pointer(i)+shift,:)= R(j:pointer(i),:);
MC(k:pointer(i)+shift,:)=MFLAG(j:pointer(i),:);
k=pointer(i)+shift+gapsvalue(i)+1;
j=pointer(i)+1;
shift=shift+gapsvalue(i);
end
RC, MC, R and MFLAG are all matrices. There are some missing data in RC and MC which will be replaced by Nans because of the R and MFLAG matrices. Now the pointer, k, j, and shift all define where the missing values are. This I know how to do. The pointer, k, j and shift are all 1 by 42 matrices. A picture of the pointer matrix is attached.
I do need help in the following:
I want to vectorize this code but I am having a hard time moving from pointer(1) = 21 to pointer (2) = 363 and so on without using the for loop. I also want to be able to do this for k, j and the shift matrices that I have.
Any help is appreciated, Thank You, M
  2 Comments
Guillaume
Guillaume on 4 Sep 2017
Rather than forcing us to spend time trying to understand what your code is doing (hint: comments are a must), why don't you explain?
Mariam Salem
Mariam Salem on 4 Sep 2017
Edited: Mariam Salem on 4 Sep 2017
Thanks for your feedback, a brief idea of what it does: It replaces some RC and MC values (that are missing, which is what the pointer defines for me, where to fill the gaps but I already did it) by values from the R and MFLAG matrices.
All I want is to be able to move from pointer(1) to pointer (2) without the use of a for loop. The same thing goes for k, j, and shift.
Please let me know if this helps in knowing what it does and if possible let me know what I can do.
Thank You.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 4 Sep 2017
It looks to me as if there is the potential for vectorizing the loop (I would need to work through the details to be certain).
However, I would not recommend vectorizing this loop. The vectorized version requires a bunch of repmat() and a bunch of logical index arithmetic, and it will difficult for most programmers to follow. You would need to be expecting to perform this operation a lot for the time spent trying to write and understand the vectorized version to be less valuable than the minor reduction in execution time. Indeed, in cases such as these, vectorization is often slower and requires more memory because of the need to construct long complicated vectors of indices and temporary values.
  4 Comments
Guillaume
Guillaume on 5 Sep 2017
@Mariam, I agree with Walter that you're unlikely to get any significant improvement out of the loop you've shown us.
However, from what you've explained that loop is only one part of the process of replacing missing values. It's possible that if you'd changed the earlier parts of that process you could get rid of the loop or have something faster. Perhaps, you should ask about that.

Sign in to comment.

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!