How can stop "while" loop
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
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
Hisham
2012년 8월 21일
The operator for the while loop is stopBool
while (~stopBool)
%do your stuff
end
José-Luis
2012년 8월 21일
Look at my original answer...
Hisham
2012년 8월 21일
José-Luis
2012년 8월 21일
You are totally right, i forgot that you need to aggregate along two dimensions. It should be sum(sum(sum....)). Modified the original posting.
Hisham
2012년 8월 22일
José-Luis
2012년 8월 22일
I think i understand what you mean. I have a question. Should the "islands" be in contact to form a path. That is, do U(1,2,1)=1 together with U(2,2,3)=1 form a path segment or not? The solution will be different whether they do or not.
Hisham
2012년 8월 22일
José-Luis
2012년 8월 22일
Use bwconncomp, if you have the image processing toolbox. If i understand correctly you want connectivity 26. Look at all the connected components and find whether any of them goes from one side to the other.
Hisham
2012년 8월 22일
Hisham
2012년 8월 22일
José-Luis
2012년 8월 22일
I think i finally got it:
conn = zeros(3,3,3);
conn(2,2,:) = 1;
conn(:,2,2) = 1;
conn(2,:,2) = 1;
CC = bwconncomp(U3,conn); labelmatrix(CC)
ans(:,:,1) =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 2
0 0 0 0 2
0 0 0 0 0
ans(:,:,2) =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 2
0 1 0 0 0
0 1 0 0 0
ans(:,:,3) =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 0 3
0 0 0 0 3
Hisham
2012년 8월 22일
Hisham
2012년 8월 22일
José-Luis
2012년 8월 22일
conn = zeros(3,3,3);
conn(:,:,2) = 1;
conn(:,2,:) = 1;
conn(2,:,:) = 1;
CC = bwconncomp(U3,conn);
[x y z] = size(U3);
isConnected = false;
while (~isConnected)
%Do your stuff here
for i = CC.PixelIdxList
[sub_x sub_y sub_z] = ind2sub([x y z],cell2mat(i));
if (~isempty(find(sub_x == 1)) && ~isempty(find(sub_x == x)) )
isConnected = true;
end
end
end
José-Luis
2012년 8월 22일
Nope, no changes, unless you want a different connectivity pattern. You can read the documentation:
doc bwconncomp;
José-Luis
2012년 8월 23일
Hisham
2012년 8월 23일
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.
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
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
