How can I merge two different tables using the first column in common?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Fernando Dos Santos
am 26 Mär. 2015
Kommentiert: Jos (10584)
am 21 Mai 2019
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 Kommentare
the cyclist
am 26 Mär. 2015
Bearbeitet: the cyclist
am 26 Mär. 2015
The rule seems to be
- merge and sort on the first column of both arrays
- put corresponding values from A into 2nd column of C
- 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]
Akzeptierte Antwort
the cyclist
am 26 Mär. 2015
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 Kommentare
Adam Danz
am 21 Mai 2019
This question came up again 4 years later but needed to work with 3 matrices. The question and answer is here:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!