Finding rows where the first column values are equal

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

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

Ah Great! Thank you so much.
Now what if I want to find rows of B that contain the same 3 numbers as in A, but in no particular order (could be column 1,2,3 or 1,2,4 or 2,3,4, or so on).
would you please explain more. i dont get you
i guess you are looking for this
[output2 B]=ismember(B,A)
For example, in array A, we have 13453,10359,9955
The current algorithm now only checks the first value, 13453.
I want it to check so that all the 3 values appear in array B but in no particular order. For example, it could be
10359,9955,X,13453
or
9955,X,13453,10359
As long as there is a combination of 4 numbers, 3 of which are these (in no particular order), it should return the index of that row.
try this:
[idx C]=ismember(B,A)
Output2=B.*idx
Thanks but this returns a Nx4 output.
The output should be Nx1 (because it is the index of rows in B that contain a combination of 3 numbers from each row of A).
I guess I can write a loop for this, but that's what I am trying to avoid (it woiuld be slow).
The loop goes like this:
Check cell 1:1 in A against rows in B. Once you find the first occurance, check all the other cells in that row for equality to cell 1:2. and if you find it, then also for cell 1:3.
If all 3 cells are found in that row, return the index.,go to cell 2:1 of A, repeat.
If not, proceed to search in the next row of B until a row is found that has cell 1:1, 1:2 and 1:3 in it.
I have 60,000+ rows in B, so this is going to be heavy.
check the previous variable 'C' or try this
output=B(idx)
Thanks, C is Nx4 as well.
idx already has the same dimensions as B. And"output" would have 4 times more rows. The correct answer should have (B rows - A rows)X1.
please format your question from this link
Then, can you show me your expected result in a proper format from the above mentioned link? atleast 2/3 rows and columns. please look at below.
13453 0 9955 10359
0 0 0 0
0 0 0 0
5257 5258 5299 0
0 0 0 0
5849 0 5848 3644
if you want a column vector result. can you show me your expected result ? atleast 5/6 columns.
13453
5257
5849
5397
17132
4767
8291
3191
Thanks a lot for your time. To begin with, I noticed that when I apply this to the whole dataset, the results are not correct:
A=readmatrix ("A.txt");
B=readmatrix ("B.txt");
Output=find(ismember(B(:,1),A(:,1)))
that's obivious. if you check carefully you will get all the elements of A(:,1) exists in B(:,1). before we worked with only some definite rows, maybe 10 to 20 rows. but B.txt and A.txt are big data file that contains severals thousands similar data to each other.
x=14990
A=readmatrix('A.txt');
B=readmatrix('B.txt');
A1=A(1:x,1);
B1=B(1:x,1);
% Output=find(ismember(B(:,1),A(:,1)));
Output2=find(ismember(B1,A1));
Output2(1:10)
try this code. until A(1:14890) and B(1:14990) the result is
1
4
6
7
9
10
11
12
13
14
but whenever its 14990, then you can get index 3. because A(14990,1)=2005 that is exists in B(3,1)
1
3
4
6
7
9
10
11
12
13
you can use ismember function to check the element of A in B
[Lia output4]=ismember(A(:,1),B(:,1));
Thank you so much. At first glance, output4 seems like the correct answer, until you sort it and see there are many zeros. For example, there is a zero in row 34 of output4. Then you go to row 34 of A and find this combination:
11191 17441 17442
This corresponds to row 74 in B:
17440 11191 17441 17442
or row 46 of output4 is zero. Combination in A:
12012 10422 14680
This corresponds to row 88 in B:
12013 12012 10422 14680
because A(34,1)=11191 does not exist in B.
A=readmatrix('A.txt');
B=readmatrix('B.txt');
% [Lia output4]=ismember(A(:,1),B(:,1));
[Lia1 output5]=ismember(B(:,1),11191);
any(output5)
ans = logical
0
see for A(33,1)=13088. it exists on B
A=readmatrix('A.txt');
B=readmatrix('B.txt');
% [Lia output4]=ismember(A(:,1),B(:,1));
[Lia1 output5]=ismember(B(:,1),13088);
any(output5)
ans = logical
1
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.
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!