Comparing multi-dimensional arrays- matlab

  • Context: MATLAB 
  • Thread starter Thread starter darthxepher
  • Start date Start date
  • Tags Tags
    Arrays Matlab
Click For Summary

Discussion Overview

The discussion revolves around comparing multi-dimensional arrays in Matlab, specifically focusing on whether two or more arrays contain the same elements. Participants explore the use of the 'all' function and logical operators for this purpose, while also clarifying terminology related to array dimensions.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in determining if two multi-dimensional arrays are identical using the 'all' function and a logical operator.
  • Another participant clarifies that the arrays in question are 2x3 matrices rather than multi-dimensional arrays, suggesting the use of the '==' operator for element-wise comparison.
  • It is noted that using 'all' in conjunction with '==' can help determine if the arrays are equivalent, with examples provided for comparisons between A, B, and C.
  • A participant confirms they successfully used 'all' but mentions the necessity of a relational operator, indicating that logical operators alone are insufficient for the task.
  • There is a minor correction regarding the interpretation of the output from the '==' operator, emphasizing the importance of understanding linear indexing in Matlab.
  • Another participant suggests an alternative approach to using 'all' by applying it to the linearized version of the arrays, A(:) and B(:).

Areas of Agreement / Disagreement

Participants generally agree on the methods for comparing arrays but express differing views on terminology and the necessity of certain operators. The discussion includes clarifications and corrections, indicating some unresolved nuances regarding array indexing and comparison techniques.

Contextual Notes

There are limitations in the discussion regarding the definitions of multi-dimensional arrays versus matrices, as well as the implications of using linear indexing in Matlab. These aspects remain somewhat ambiguous and are not fully resolved.

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(:))
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
0
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K