2
votes

I have two matrix. Both are nx2 with values column 1 has id and column 2 has values:

| id1 | A1 |
| id2 | A2 |
| id3 | A3 |
| id4 | A4 |

First one represents data about object A and second one represents data about object B. I need to find common ids from both matrix and put them into a single matrix with just values:

| A1 | B1 |
| A2 | B2 |
| A3 | B3 |
| A4 | B4 |

Condition being each row represents values corresponding to same id. Important: Ignore all ids which are not common to both input matrix. My data set is long and I am looking at best way to accomplish it in octave (similar solution would work with matlab as well I guess). Ids in a single matrix are unique.

Note: I forgot to mention earlier that id's (for a single matrix) are unique to a matrix but they don't represent row numbers and numbers with no meaning.

1
yes ids are unique in a matrix. I will add that point. - user648129

1 Answers

4
votes

This is most easily accomplished with intersect:

[uniqueIDs, idxA, idxB] = intersect(A(:,1),B(:,1));

result = [A(idxA,2),B(idxB,2)]