how to merge two matrices via one common column

5 ビュー (過去 30 日間)
Xin CUI
Xin CUI 2013 年 6 月 23 日
Hi there. My question is that I have two matrices, say, A and B, A have two columns, 1 and 2, B has two columns, 3 and 4. Columns 2 and 3 are actually same numbers, BUT with difference order. How can I merge them together? For example, column 2 of matrix A is [a b c d e f]' and column 3 of matrix B is [d c b a e f]'. All the letters are unique ones. Is there an easy way to merge A and B?
  1 件のコメント
Matt J
Matt J 2013 年 6 月 23 日
編集済み: Matt J 2013 年 6 月 23 日
It is confusing to call the columns of B "3 and 4" if B only has two columns.

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 6 月 23 日
[~,ii] = ismember(A(:,2),B(:,1));
out = [A, B(ii,2)];
  1 件のコメント
Xin CUI
Xin CUI 2013 年 6 月 23 日
Thank you. Works very well.

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

その他の回答 (1 件)

Matt J
Matt J 2013 年 6 月 23 日
編集済み: Matt J 2013 年 6 月 23 日
Anew=sortrows(A,2);
Bnew=sortrows(B,1);
merged=[Anew,Bnew(:,2)];
  1 件のコメント
Xin CUI
Xin CUI 2013 年 6 月 23 日
Works great. Thank you.

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by