Linking values in two matrices

Hi, Complete Newbie!
I want to compare two matrices let's say A and B if A = [1 0 3 0 2 5 ] and B = [0 2 5 0 2 10 ]. I need to work out for any cell in A>0 is the corresponding cell in B filled (i.e. >0) (for now it doesn't matter what the difference in value is, but just whether the cells are filled or not). In addition, I want to know if a cell in A is equal to 0 is the corresponding cell in B also equal to 0. I then want to count the number of times that the cells match i.e. how many times in A and B were both the cells filled or both = 0?
Any suggestions much appreciated!

Answers (1)

Adam
Adam on 8 May 2017
Edited: Adam on 8 May 2017
A > 0 & B > 0
A == 0 & B == 0
Counting the results is trivial.

6 Comments

Thanks Adam. I had seen this before. If I understand correctly this simply tells me if the values are equal which sorts out the 0 value problem but doesn't tell me if the same cells are filled. I am looking at this from a spatial perspective. My aim is that at the end I can say X out of the total number of cells are filled in both A and B...
For example, from this example I would like an answer like this:
0 0 1 1 1 1
where 0 means both cells are both empty and 1 means both cells have values >0. That way I can say 4 out of 6 cells are filled in both cases so they have a match rate of ~66%.
I am guessing I have to do a loop or set it up so matlab counts each row or column?
You can combine the tests:
match = (A > 0) == (B > 0);
You mention filled is A > 0, and you mention 0. If you are either sure that you will never have negatives, or if you want negatives to be considered filled as well, then you can use
match = logical(A) == logical(B)
and if you do not mind being a bit obscure, that can be written
match = ~A == ~B;
Thanks Walter, this seems to work for me. Thank you!
"if I understand correctly this simply tells me if the values are equal"
No, A > 0 & B > 0 is true in locations where both items are filled. A > 0 is 0 for empty items and 1 for filled items, and the & operator is true only if both corresponding items are non-zero .
In the case where you do not have negative values, A > 0 & B > 0 can be abbreviated as A & B, which means the same thing as (A ~= 0) & (B ~= 0)
Aoife Cordon
Aoife Cordon on 9 May 2017
Edited: Aoife Cordon on 9 May 2017
Hey Walter, Thank you for explaining this. Is there a way of only comparing certain cells in A with the corresponding cells in B? I only want it to compare cells in A>0 and see if the corresponding cells in B are also filled? For example, in the above example, I only want it to compare the four cells which are filled in A with the corresponding cells in B.
"I only want it to compare the four cells which are filled in A with the corresponding cells in B"
B(A>0)>0

Sign in to comment.

Asked:

on 8 May 2017

Commented:

on 9 May 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!