Compare two tables and extract data

Hello, I have two data matrix: A and B
A =
101 62
102 65
103 62
104 58
105 72
106 80
B =
99 65
100 62
102 80
104 71
105 73
109 62
I would like to search all same values in first column of A and B, and create a column C with all the extracted rows from A and B :
C =
102 65 80
104 58 71
105 72 73
Any help is very much appreciated!

1 件のコメント

MELO
MELO 2020 年 10 月 12 日
C = B(ismember(B(:,1),A),:)
I know that with this code, I can only return B, but I also need the second column of A.
I thought about indices?

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 12 日
編集済み: Ameer Hamza 2020 年 10 月 12 日

1 投票

Try this
A = [
101 62
102 65
103 62
104 58
105 72
106 80];
B = [
99 65
100 62
102 80
104 71
105 73
109 62];
[idxA, idxB] = ismember(A(:,1), B(:,1));
C = [A(idxA, :) B(idxB(idxB~=0), 2)];

4 件のコメント

MELO
MELO 2020 年 10 月 12 日
Thank you very much, it worked perfectly ^^
Ameer Hamza
Ameer Hamza 2020 年 10 月 12 日
I am glad to be of help!!!
MELO
MELO 2020 年 10 月 13 日
you'are so nice. I love it!
Ameer Hamza
Ameer Hamza 2020 年 10 月 13 日
My pleasure! You may accept the answer to show appreciation.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2020 年 10 月 12 日

コメント済み:

2020 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by