# How to break with two conditions

조회 수: 3(최근 30일)
Moe 2014년 11월 26일
댓글: Adam 2014년 12월 1일
I want to break my code
when both m and m2m is smaller than 0
I used the following command, but it gave me error:
if m(:,:,j) < 0 AND m2m(:,:,j) < 0
break % get out of the for-loop
end
% Undefined function 'AND' for input arguments of type 'char'.
% Error in new55 (line 340)
if m(:,:,j) < 0 AND
m2m(:,:,j) < 0
##### 댓글 수: 3표시숨기기 이전 댓글 수: 2
Star Strider 2014년 12월 1일
Thank you!

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

### 답변(2개)

Image Analyst 2014년 11월 26일
m(:,:,j) and m2(:,:,j) are not single numbers. They're 2D arrays. I suggest you wrap them in any() or all(), depending on what you're wanting - any element to meet the criteria or all of the elements in the 2D matrix to meet the criteria. Plus use && instead of AND:
if all(m(:,:,j)) < 0 && all(m2m(:,:,j)) < 0
##### 댓글 수: 6표시숨기기 이전 댓글 수: 5
Adam 2014년 12월 1일
Yeah, I guess a squeeze may be needed and maybe an all( all( ... ) ).
I created functions called
column(x)
row(x)
in our Matlab library that just warp up the functionality for making a matrix a column ( i.e. the (:) notation ) and a row (transpose of column notation) so that I can use them in situations like this because Matlab does not allow syntax like:
m(:,:,j)(:)
which is what you'd need to do to achieve the same thing in non-function syntax.

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

Andrew Reibold 2014년 11월 26일
Instead of AND, use &&
if m(:,:,j) < 0 && m2m(:,:,j) < 0
'AND' will not work. Make that change and start debugging from there.
##### 댓글 수: 1표시숨기기 없음
Stephen23 2014년 12월 1일
As Image Analyst points out " m(:,:,j) and m2(:,:,j) are not single numbers. They're 2D arrays", so the scalar operator && will not work like that. These arrays need to be wrapped in an all or any (or something similar) before applying the && operator:
if all(m(:,:,j) < 0 ) && all(m2m(:,:,j) < 0 )

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

### Community Treasure Hunt

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

Start Hunting!