How can I change for-loop into matrix way?
이전 댓글 표시
I'd like to vectorize the for-loop below, but cannot come up with any idea. Could you help me?
for i=1:100
for j=1:1000
A(i+1,j)= B(find(C(B==A(i,j),:,D==E(i))>F(i,j),1));
end
end
댓글 수: 9
Jan
2021년 6월 7일
Are you sure, that your 2nd loop is "for j=1000"?
icdi
2021년 6월 7일
@icdi Jan means, shouldn't you have this instead:
for j=1:1000
With " for j=1000" the inner loop is only doing 1 iteration for every i.
Matt J
2021년 6월 7일
We also need to know the sizes of all the variables.
And D is 3x1?
Also, is the loop working in its current form? As far as I can see, there is no reason why
find(C(B==A(i,j),:,D==E(i))>F(i,j),1)
should be gauranteed to give a number between 1 and 15, as the code assumes it will. This will only be true if there is a unique m with B(m)=A(i,j) and a unique n with D(n)=E(i).
So the elements of B and D are all unique? Also, what is supposed to happen if find() returns empty [], i.e., if
C(B==A(i,j),k,D==E(i)) < F(i,j)
for all k=1...15?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!