# How can stop "while" loop

4 views (last 30 days)
Hisham on 21 Aug 2012
Hi, I have a while loop, my code is inside the loop. I want to stop the loop when the same number (must be non zero)created in the matrix from the first row to the last row.
ex.
this matrix is inside the while loop:
U3(:,:,1) = [
0 , 0 , 0 , 1 , 0;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
U3(:,:,2) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 1 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 0
]
U3(:,:,3) = [
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0;
0 , 1 , 0 , 0 , 1;
0 , 0 , 0 , 0 , 0;
0 , 0 , 0 , 0 , 0
]
Now, the loop will continue but if U3(1,2, 1) becomes equal to 1, the loop will stop. How can I do it?
Note:
The bath from the first row to the last row can go in any direction (x,y,z) but it must pass from the topped row to the bottom row similar to the above example. The bath from the topped row to the bottom row is as:
U3(1,2,1), U3(2,2,1), U3(3,2,1), U3(3,2,2), U3(4,2,2), U3(5,2,2) and U3(3,2,3) ONLY

Hisham on 21 Aug 2012
But how can I do it for 3D matrix?
Note: I might have more than an island, but I want to stop the while loop when any of the islands created a path from the topped row to the bottom row.

José-Luis on 21 Aug 2012
Edited: José-Luis on 22 Aug 2012
[x y z] = size(U3);
condZ = sum(sum(sum(U3,3) == z)) > 0;
condY = sum(sum(sum(U3,2) == y)) > 0;
condX = sum(sum(sum(U3) == x)) > 0;
stopBool = condX || condY || condZ;
Cheers!

Hisham on 23 Aug 2012
figure
hpat = PATCH_3Darray(gridINPUT,gridX,gridY,gridZ);
But the issue is about how can I put U3 matrix in the code.
Do I need to change "gridINPUT" to "U3"? or how to plot the U£ matrix?
José-Luis on 23 Aug 2012
Hisham, I would strongly recommend reading the getting started part of the documentation. grinInput, gridX, gridY, and gridZ are just placeholder variable names that are passed to the function PATCH_3Darray. You can name them whatever you want. In this case, yes, gridINPUT would be U3. Copy/pasting from the function description:
gridX (optional) - A 1xP array - List of the X axis coordinates.
gridY (optional) - A 1xQ array - List of the Y axis coordinates.
gridZ (optional) - A 1xR array - List of the Z axis coordinates.
That means that passing PATCH_3Darray(U3) should be enough to produce some results.
Hisham on 23 Aug 2012
I tried the above function but it is suitable for my case. Do you know any function or way to plot similar to the link below?
I did it for 2D by using the following code:
[r,c]=size(U3);
figure
for cIdx=1:c,
y=find(U3(:,cIdx)==1);
if length(y)>1
y=(r+1)-y;
x=ones(size(y))*cIdx;
for idx=1:length(x)-1
if abs( y(idx) - y(idx+1))<=1
line(x(idx:idx+1),y(idx:idx+1),'LineWidth',4)
end
end
hold on
end
end
for rIdx=1:r,
x=find(U3(rIdx,:)==1);
if length(x)>1
y=(r+1)-ones(size(x))*rIdx;
for idx=1:length(x)-1
if abs( x(idx) - x(idx+1))<=1
line(x(idx:idx+1),y(idx:idx+1),'LineWidth',4)
end
end
hold on
end
end
It is very complicated to do it for 3D. Do you know any function able to do it?
Thanks!

Azzi Abdelmalek on 21 Aug 2012
test=1
while condition1 & test==1
%if you want exit a loop change the value of test for example
test=0, %any value different from 1

#### 1 Comment

Hisham on 22 Aug 2012
It is very nice, but my problem is how can I build the condition that should be valid for a 3D matrix.