MATLAB Answers

How can stop "while" loop

4 views (last 30 days)
Hisham
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

  0 Comments

Sign in to comment.

Accepted Answer

Hisham
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.

  0 Comments

Sign in to comment.

More Answers (2)

José-Luis
José-Luis on 21 Aug 2012
Edited: José-Luis on 22 Aug 2012
Your condition should be
[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!

  27 Comments

Hisham
Hisham on 23 Aug 2012
I downloaded the file from the above link. Then I used this code:
load exampleA.mat
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
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
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!

Sign in to comment.


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

  1 Comment

Hisham
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.
can you help me more...please

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!