how to stop from running a program whenever any of the element become zero or gets negative?
조회 수: 4 (최근 30일)
이전 댓글 표시
i have a matrix A=rand(m,n); B=1-A; so when B is running in a loop for 100 times i want to end the matrix once any of the element of B becomes zero.
plz plz help me....
댓글 수: 1
Image Analyst
2014년 5월 10일
B will never be exactly 0 since A will never be exactly 1, at least probably not in your lifetime.
채택된 답변
Sara
2014년 5월 10일
If you want to exit the loop, you can do:
if(ismember(0,B))
break
end
If there is code after the loop, that part will be executed though. Do you have code after the loop that you do NOT want to execute?
추가 답변 (1개)
Star Strider
2014년 5월 10일
Use the any and break functions.
Example:
for k1 = 1:100
A = rand(10);
B = 1-A;
if any(B == 0)
k1
break
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!