How can I merge two different tables using the first column in common?

1 次查看(过去 30 天)
A(class double)
[1 7;
3 15]
B(class double)
[2 9;
5 10]
C = (A+B)
[1 7 0;
2 9 0;
3 15 0;
4 0 0;
5 0 10]
  3 个评论
the cyclist
the cyclist 2015-3-26
编辑:the cyclist 2015-3-26
The rule seems to be
  1. merge and sort on the first column of both arrays
  2. put corresponding values from A into 2nd column of C
  3. put corresponding values from B into 3rd column of C
I think that the second row of C is supposed to be
[2 0 9]

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2015-3-26
A = [1 7;
3 15]
B = [2 9;
5 10]
Ca = [A, zeros(size(A,1),1)];
Cb = [B(:,1), zeros(size(B,1),1), B(:,2)];
M1 = union(A(:,1),B(:,1));
M2 = setdiff((1:max(M1))',M1);
M3 = [M2, zeros(size(M2,1),2)];
C = sortrows([Ca; Cb; M3],[1]);
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by