How can stop "while" loop

조회 수: 10 (최근 30일)
Hisham
Hisham 2012년 8월 21일
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
Hisham 2012년 8월 21일
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.

추가 답변 (2개)

José-Luis
José-Luis 2012년 8월 21일
편집: José-Luis 2012년 8월 22일
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
José-Luis
José-Luis 2012년 8월 23일
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 2012년 8월 23일
편집: Hisham 2012년 8월 23일
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
Azzi Abdelmalek 2012년 8월 21일
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
Hisham
Hisham 2012년 8월 22일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by