简体   繁体   中英

intersecting two matrix in octave

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.

This is most easily accomplished with intersect :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM