Reputation: 532
I have a matrix which only consists of 0s and 1s. I want to make a nested loop which checks the consecutive 0s in my matrix and prints out the number as the distance . I will use the distance later to compute distance beetween points of the matrix.
Here's my code and my test matrix B.
B = [ 1 1 1 0 0 0 1
0 0 0 1 1 1 1];
for i=1:2
for j=1:7
if B(i,j)==0
jtemp=j;
distance=0;
while B(i,jtemp)==0
jtemp=jtemp+1;
distance=distance+1;
end
fprintf('%0.0f,The distance is\n',distance)
end
end
end
When I run this code I get something like this :
3,The distance is
2,The distance is
1,The distance is
3,The distance is
2,The distance is
1,The distance is
So my question is why this code doesn't print the distance by calculating the consecutive 0s in a row of the matrix
Upvotes: 0
Views: 76
Reputation: 7751
This behavior is due to the successive calls of j
(from 1 to 7, whatever the value of jtemp is). You may insert a condition (if j<jtemp
) that tells matlab not to process further (continue
) until j
matches jtemp
again.
for i=1:2
jtemp = 1;
for j=1:7
if j<jtemp
continue
end
Upvotes: 1