Finding rows where the first column values are equal

조회 수: 24 (최근 30일)
Pelajar UM
Pelajar UM 2022년 3월 7일
댓글: Pelajar UM 2022년 3월 8일
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));

채택된 답변

Arif Hoq
Arif Hoq 2022년 3월 7일
편집: Arif Hoq 2022년 3월 7일
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
Pelajar UM
Pelajar UM 2022년 3월 8일
편집: Pelajar UM 2022년 3월 8일
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 2022년 3월 8일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by