MATLAB Comparing multi-dimensional arrays- matlab

  • Thread starter Thread starter darthxepher
  • Start date Start date
  • Tags Tags
    Arrays Matlab
AI Thread Summary
To compare multi-dimensional arrays in MATLAB, specifically 2x3 matrices, the correct approach involves using the equality operator (==) for element-wise comparison. For example, comparing matrices A and B reveals which elements differ, while comparing B and C shows they are equivalent. The `all` function can be applied to determine if all elements in corresponding columns are equal. To achieve a single logical value indicating overall equality, you can nest the `all` function, such as using `all(all(A == B))`, which returns 0 if they are not equal and 1 if they are. It's important to note that true multi-dimensional arrays have three or more dimensions, and linear indexing can enhance performance in certain situations. Using `all(A(:) == B(:))` is a more efficient method for comparison.
darthxepher
Messages
56
Reaction score
0
Comparing multi-dimensional arrays-- matlab

So I need to be able to tell if these multi dimensional arrays have the same numbers using the all function and one logical operator:

A = [ 5 6 8; 8 0 0];
B = [ 5 7 8; 8 0 0];
C = [ 5 7 8; 8 0 0];

I am stuck and can't figure out a way!

Some hints perhaps?

Thanks.
 
Physics news on Phys.org


darthxepher said:
So I need to be able to tell if these multi dimensional arrays have the same numbers using the all function and one logical operator:

A = [ 5 6 8; 8 0 0];
B = [ 5 7 8; 8 0 0];
C = [ 5 7 8; 8 0 0];

I am stuck and can't figure out a way!

Some hints perhaps?

Thanks.

First, those aren't really "multi-dimensional" arrays; they're 2x3 arrays, which are referred to simply as arrays or matrices in Matlab parlance. A multi-dimensional array is generally something with three or more dimensions.

Next, in order to compare two matrices you should use the == operator. In Matlab this operator will perform an element-by-element comparison. So, if you compare A to B you obtain

Code:
>> A == B

ans =

     1     0     1
     1     1     1

which tells you that they are identical apart from the elements A(3) and B(3). On the other hand, if you compare B with C:

Code:
>> B == C

ans =

     1     1     1
     1     1     1

you see that B and C are equivalent.

Next, the all function, when acting on matrices, returns TRUE if none of the elements in each column are zero. We can combine this with the == operator to determine whether, say, the columns of A and B are equivalent:

Code:
>> all(A == B)

ans =

     1     0     1

This tells us that the columns in A are the same as those in B apart from the second column. To reduce this to just a single logical value, we apply all again:

Code:
>> all(all(A == B))

ans =

     0

Hence, A is not equal to B. On the other hand, we can apply the same idea to show that B is equal to C:

Code:
>> all(all(B == C))

ans =

     1
 


ok you I did it that way... i did all(A==B) or all(B==C). It seems you cannot do it just with logical operators but you do need to utilize a relational operator in order to do so. ! thx!
 


shoehorn said:
Next, in order to compare two matrices you should use the == operator. In Matlab this operator will perform an element-by-element comparison. So, if you compare A to B you obtain

Code:
>> A == B

ans =

     1     0     1
     1     1     1

which tells you that they are identical apart from the elements A(3) and B(3).
Minor correction: It's saying that A(1,2) and B(1,2) are unequal.
 


Mark44 said:
Minor correction: It's saying that A(1,2) and B(1,2) are unequal.

Which is what A(3) and B(3) are! This is called linear indexing of an array in Matlab; check the docs for why and how you can index an array using a single scalar argument.

(BTW: this is not simply an obscure point of style; there exist situations, mostly related to the vectorization of code, where linear indexing results in faster execution.)
 


Along those same lines, instead of using all twice, you can just use all(A(:) == B(:))
 
Back
Top