Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Comparing multi-dimensional arrays- matlab

  1. Jul 6, 2010 #1
    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.
     
  2. jcsd
  3. Jul 6, 2010 #2
    Re: Comparing multi-dimensional arrays-- matlab

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

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

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

    >> all(all(B == C))

    ans =

         1
     
  4. Jul 6, 2010 #3
    Re: Comparing multi-dimensional arrays-- matlab

    ok ya 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!
     
  5. Jul 7, 2010 #4

    Mark44

    Staff: Mentor

    Re: Comparing multi-dimensional arrays-- matlab

    Minor correction: It's saying that A(1,2) and B(1,2) are unequal.
     
  6. Jul 7, 2010 #5
    Re: Comparing multi-dimensional arrays-- matlab

    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.)
     
  7. Jul 8, 2010 #6
    Re: Comparing multi-dimensional arrays-- matlab

    Along those same lines, instead of using all twice, you can just use all(A(:) == B(:))
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook