Finding rows where the first column values are equal

15 views (last 30 days)
Array A:
13453 10359 9955
5257 5299 5258
5849 3644 5848
5397 7230 5396
17132 17130 17118
4767 4768 4770
8291 8292 8316
3191 3190 15970
13389 14409 14410
14840 13022 13021
Array B:
13453 12292 9955 10359
6805 3039 3012 3011
2005 8087 2394 17740
5257 5258 5299 2012
6309 6290 6310 6289
5849 5811 5848 3644
5397 7230 8425 5396
4775 4760 6529 1118
17132 17130 13938 17118
4767 4791 4770 4768
8291 8292 5145 8316
3191 3190 14432 15970
10539 14895 9170 10540
13389 14409 9927 14410
14840 14387 13021 13022
Expected output: Indices of rows in Array B where the first column value is the same as Array A:
1
4
6
7
9
10
11
12
14
15
When I try to use ismember, I get an error that "arrays have incompatible sizes".
x = find (B(:,1) == A (:,1));

Accepted Answer

Arif Hoq
Arif Hoq on 7 Mar 2022
Edited: Arif Hoq on 7 Mar 2022
A=[13453 10359 9955
5257 5299 5258
5849 3644 5848
5397 7230 5396
17132 17130 17118
4767 4768 4770
8291 8292 8316
3191 3190 15970
13389 14409 14410
14840 13022 13021];
B=[13453 12292 9955 10359
6805 3039 3012 3011
2005 8087 2394 17740
5257 5258 5299 2012
6309 6290 6310 6289
5849 5811 5848 3644
5397 7230 8425 5396
4775 4760 6529 1118
17132 17130 13938 17118
4767 4791 4770 4768
8291 8292 5145 8316
3191 3190 14432 15970
10539 14895 9170 10540
13389 14409 9927 14410
14840 14387 13021 13022];
Output=find(ismember(B(:,1),A(:,1)))
Output = 10×1
1 4 6 7 9 10 11 12 14 15
  16 Comments
Pelajar UM
Pelajar UM on 8 Mar 2022
Edited: Pelajar UM on 8 Mar 2022
Right. But is there a way that I could look for a combination of 3 numbers at once. If I want to look column by column, that would need a loop.
More importantly, the number in column 2 of A may not appear in column 2 of B, it could be in column 3. Meaning that I have to look at 3x4 possibilities.
I think one shorcut would be to only look at column 2 if the index was 0 for column 1. There are 719 zeroes right now, this is more manageable.
Pelajar UM
Pelajar UM on 8 Mar 2022
Something like this:
[Lia1, output1]=ismember(A(:,1),B(:,1));
idx1=find(all(Lia1==0,2));
[Lia2, output2]=ismember(A(:,1),B(:,2));
idx2=output2(idx1,:);
But I cannot figure out why there are sitll some zero values in idx2. For example row 13 is zero, this corresponds to 492 in idx1.
Row 492 in A is
3177 3325 3178
which should correspond to row 836 in B:
7137 3177 3325 3178
This only happens for 13 rows. The rest looks good.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!