Vectorizating a for loop

조회 수: 3 (최근 30일)
Mariam Salem
Mariam Salem 2017년 9월 4일
댓글: Walter Roberson 2017년 9월 5일
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
Guillaume
Guillaume 2017년 9월 4일
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 2017년 9월 4일
편집: Mariam Salem 2017년 9월 4일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 9월 4일
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
Guillaume
Guillaume 2017년 9월 5일
@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.

댓글을 달려면 로그인하십시오.

카테고리

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