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
diff
andfind
. – mbaumanfprintf
command seems malformed. Try this instead:fprintf('The distance is %g\n',distance)
– chappjc