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:
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!
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.
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.)