简体   繁体   中英

Find the rows member of matrix in another matrix in Matlab

I have two matrices as follow. I want to check the any member of rows in A which is in the B. For example, If I chose the second row in A(2) [7 12 a], since 12 is in the B(3), it gives me [12 13 b] or index 3. Or if I chose row 1 of A, since A(1)=[ 5 3 a], and 5 in the second row of B. The code gives me 2 or [5 10 b].

a= 100;
b= 200;

A= [5 3 a;
    7 12 a;
    9 10 a];
B =[1 6 b;
    5 10 b;
    12 13 b;
10 13 b];

I have used the ismember function, but I can not find the results. In summary: row_chosen_from_A = 3 I want the index of B which includes any number of row_chosen_from_A. results = 2, 4

You can do it with '[LIA,LOCB] = ismember(A,B)' .

LOCB shows the row number of B for each match. In your example the result for LOCB is:

LOCB =

 2     0     0
 0     3     0
 0     4     0

It means 5 in A is found in the second row of B , 12 in the third and 10 in the fourth.

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