How can I change for-loop into matrix way?
Show older comments
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 Comments
Jan
on 7 Jun 2021
Are you sure, that your 2nd loop is "for j=1000"?
icdi
on 7 Jun 2021
@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
on 7 Jun 2021
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?
Accepted Answer
More Answers (0)
Categories
Find more on Blue 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!