Comparing multi-dimensional arrays- matlab

In summary, using the all function and the == operator on two matrices will tell you if they are equivalent, whether or not their columns are indexed in a linear fashion, and whether or not any elements in one column are zero.
  • #1
darthxepher
56
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
  • #2


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
 
  • #3


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!
 
  • #4


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.
 
  • #5


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.)
 
  • #6


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

1. What is a multi-dimensional array in Matlab?

A multi-dimensional array in Matlab is a data structure that can store values in more than one dimension. It is similar to a matrix, with rows and columns, but can have additional dimensions, such as a third dimension for storing data over time.

2. How do I create a multi-dimensional array in Matlab?

To create a multi-dimensional array in Matlab, you can use the "zeros" or "ones" function, which will create an array of the specified size filled with zeros or ones, respectively. You can also use the "rand" function to create an array with random values, or the "eye" function to create an identity matrix.

3. How do I access elements in a multi-dimensional array in Matlab?

You can access elements in a multi-dimensional array in Matlab using the same indexing notation as a matrix, but with additional dimensions specified. For example, to access the element in the first row, second column, and third dimension, you would use array(1,2,3).

4. How can I compare two multi-dimensional arrays in Matlab?

To compare two multi-dimensional arrays in Matlab, you can use the "isequal" function, which will return a logical value indicating whether the two arrays are equal in size and have the same values. You can also use the "all" function to check if all elements in two arrays are equal.

5. Can I perform mathematical operations on multi-dimensional arrays in Matlab?

Yes, you can perform mathematical operations on multi-dimensional arrays in Matlab, just like you would on a matrix. Matlab automatically performs element-wise operations on arrays with the same dimensions. If you want to perform operations on arrays with different dimensions, you can use the "bsxfun" function to expand the dimensions of one array to match the other.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
743
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top