How can stop "while" loop
Ältere Kommentare anzeigen
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
Akzeptierte Antwort
Weitere Antworten (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 Kommentare
Hisham
am 21 Aug. 2012
The operator for the while loop is stopBool
while (~stopBool)
%do your stuff
end
José-Luis
am 21 Aug. 2012
Look at my original answer...
Hisham
am 21 Aug. 2012
José-Luis
am 21 Aug. 2012
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
am 22 Aug. 2012
José-Luis
am 22 Aug. 2012
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
am 22 Aug. 2012
José-Luis
am 22 Aug. 2012
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
am 22 Aug. 2012
Hisham
am 22 Aug. 2012
José-Luis
am 22 Aug. 2012
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
am 22 Aug. 2012
Hisham
am 22 Aug. 2012
José-Luis
am 22 Aug. 2012
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
am 22 Aug. 2012
Nope, no changes, unless you want a different connectivity pattern. You can read the documentation:
doc bwconncomp;
José-Luis
am 23 Aug. 2012
Hisham
am 23 Aug. 2012
José-Luis
am 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.
Azzi Abdelmalek
am 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 Kommentar
Hisham
am 22 Aug. 2012
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
