How to break with two conditions

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 个评论

You need to describe in a bit more detail what you want to do. It might help if you provided a context for it, specifically how you calculated the matrices and what you are doing with them.
The problem is that you are testing two (MxN) matrices, ‘m(:,:,j)’ and ‘m2m(:,:,j)’. Do you want all the elements to be <0, any of them to be <0, the sum of them to be <0, the product of them to be <0, the determinant to be <0, or something else?
+1 Star Strider for listing more than just and and all.

请先登录,再进行评论。

回答(2 个)

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 个评论

Operands to the and && operators must be convertible to logical scalar values.
It looks like it can't be done all in one step like I thought it could. Try it this way instead
mSlice = m(:,:,j) < 0; % Logical array
m2Slice = m(:,:,j) < 0;
if sum(mSlice(:)) < 0 && sum(m2Slice(:)) < 0
% All elements are negative.
continue;
end
I guess the code you gave is not correct. Because, sum up of arrays can be negative, but still there is/are some positive arrays in matrix. For example:
m = [1;-20;-3;4;5];
a = sum(m)
ans = -13 % based on your code, my code should be stopped. But, in fact it should not, because there are still 3 positive arrays
if all(m(:,:,j) < 0 ) && all(m2m(:,:,j) < 0 )
should work with the parentheses corrected from the original answer
Mohammad, my code works. The problem is you did not try it. You modified it and broke it. You summed m . I did not do that. I did not sum m. I summed a logical array , gotten by comparing m to 0, and that array has only 0's and 1's in it. Look at this example with your simplified m:
m = [1;-20;-3;4;5];
mSlice = m < 0 % Logical array
In the command window:
mSlice =
0
1
1
0
0
See, mSlice has only 0 and 1 so that when you sum it, there is no way possible that you can get a negative number . Try it again, like I said this time comparing it to 0.
By the way, Adam's way doesn't work either for the same reason mine didn't - all() operating on a 3D array gives a row vector, not a true or false answer. It seems like it should (and that's why I originally proposed it, but when I actually tested it, it didn't.
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.

请先登录,再进行评论。

Instead of AND, use &&
if m(:,:,j) < 0 && m2m(:,:,j) < 0
'AND' will not work. Make that change and start debugging from there.

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 )

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrices and Arrays 的更多信息

提问:

Moe
2014-11-26

评论:

2014-12-1

Community Treasure Hunt

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

Start Hunting!

Translated by