Matlab: if the condition when the two-dimensionality of two matrices is not equal

I have two matrices that depend on the choice, there will be some other calculations. For example, suppose:

A = 8 9 3 9 6 5 2 1 9 

and

 B = 11 9 11 8 2 2 2 8 9 8 11 5 1 9 1 11 11 10 5 4 6 9 11 8 1 

Now I would choose one of them as the new matirix

 C = A; C = B; 

If I use the following if condition, I will have an error.

 if C==A %do some computation else if C == B %do some other computation else %print an error 

Since the dimension of the matrices is not equal, then I have an error. Could you tell me how I could formulate this correctly?

+5
source share
2 answers

MATLAB provides a function to evaluate the equality of an array: isequal .

So try:

 if isequal(C,A) %do some computation else if isequal(C,B) %do some other computation else %print an error 
+5
source

You can add another condition for the size, note that if the size condition is not met, the second condition is not checked, so you will not get an error:

 if all(size(C)==size(A)) && all(C==A) %do some computation elseif all(size(C)==size(B)) && all(C == B) %do some other computation else %print an error end 

Note that the condition must be all(C==A) .

+3
source

Source: https://habr.com/ru/post/1213696/


All Articles