Filter löschen
Filter löschen

how to merge two matrices via one common column

13 Ansichten (letzte 30 Tage)
Xin CUI
Xin CUI am 23 Jun. 2013
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 Kommentar
Matt J
Matt J am 23 Jun. 2013
Bearbeitet: Matt J am 23 Jun. 2013
It is confusing to call the columns of B "3 and 4" if B only has two columns.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 23 Jun. 2013
[~,ii] = ismember(A(:,2),B(:,1));
out = [A, B(ii,2)];

Weitere Antworten (1)

Matt J
Matt J am 23 Jun. 2013
Bearbeitet: Matt J am 23 Jun. 2013
Anew=sortrows(A,2);
Bnew=sortrows(B,1);
merged=[Anew,Bnew(:,2)];

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by