Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

how to merge two matrices via one common column

Asked by Xin CUI on 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 Comment

Matt J on 23 Jun 2013

It is confusing to call the columns of B "3 and 4" if B only has two columns.




No products are associated with this question.

2 Answers

Answer by Andrei Bobrov on 23 Jun 2013
Accepted answer
[~,ii] = ismember(A(:,2),B(:,1));
out = [A, B(ii,2)];

1 Comment

Xin CUI on 23 Jun 2013

Thank you. Works very well.

Andrei Bobrov
Answer by Matt J on 23 Jun 2013
Edited by Matt J on 23 Jun 2013

1 Comment

Xin CUI on 23 Jun 2013

Works great. Thank you.

Matt J

Contact us