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

1 回表示 (過去 30 日間)
elmar.a
elmar.a 2019 年 5 月 21 日
コメント済み: Jos (10584) 2019 年 5 月 22 日
This question is related to How can I merge two different tables using the first column in common? but the accepted answer does not fully solve my issue.
I have a case where there are more than two arrays, some of which have the same identifier in the first column, such as
A = [1 7;
3 15]
B = [2 9;
5 10]
C = [2 5;
3 4]
From this I'd like to get
[1 7 0 0;
2 0 9 5;
3 15 0 4;
4 0 0 0;
5 0 10 0]
that means if identifiers are the same (as for row 3) the values of A, B, and C should appear in the same row.
  2 件のコメント
madhan ravi
madhan ravi 2019 年 5 月 21 日
Not clear, more explanation needed.
Adam Danz
Adam Danz 2019 年 5 月 21 日
Column 1 is just a row index number.
Column 2 are the number is col 2 of "A". Column 3 are the numbers in col 2 of "B". Column 4 are the numbers in col 2 of "C".

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

採用された回答

Jos (10584)
Jos (10584) 2019 年 5 月 21 日
% data, (showing the drawback of storing relates things in different variables)
A = [1 7;
3 15]
B = [2 9;
5 10]
C = [2 5;
3 4]
% simple indexing engine
A(:,3) = 2, B(:,3) = 3, C(:,3) = 4 % add column numbers to input
D = cat(1,A,B,C)
sz = [max(D(:,1)), D(end,3)]
m = zeros(sz)
m(D(:,1), 1) = D(:,1)
m(sub2ind(sz, D(:,1), D(:,3))) = D(:,2)
  1 件のコメント
elmar.a
elmar.a 2019 年 5 月 22 日
Thanks, that does the job. I like this short solution!

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

その他の回答 (2 件)

Adam Danz
Adam Danz 2019 年 5 月 21 日
編集済み: Adam Danz 2019 年 5 月 21 日
I find it easier to first combine the matrices into a 3D array. This should work with any number of matrices as long as they are the same size. "m" is your final matrix.
ABC = cat(3,A,B,C);
% Create final matrix (all 0s except first column)
m = zeros(max(ABC(:,1,:),[],'all'),size(ABC,3)+1); %prior to r2018b: zeros(max(max(squeeze(ABC(:,1,:)))),size(ABC,3)+1)
m(:,1) = 1:size(m,1);
% Find the column and row indices of M for the elements in ABC
colID = reshape(repelem(2:size(ABC,3)+1,size(ABC,2),size(ABC,2)-1,1),[],1);
[~, rowID] = ismember(reshape(ABC(:,1,:),[],1),m(:,1));
% fill in the rest of the m matrix
m(sub2ind(size(m),rowID,colID)) = ABC(:,2:end,:);
Result
m =
1 7 0 0
2 0 9 5
3 15 0 4
4 0 0 0
5 0 10 0
  5 件のコメント
Adam Danz
Adam Danz 2019 年 5 月 22 日
Yeah, I mentioned that in the answer (2nd sentence).
Jos' answer is more fleixible because it allows for varying number of rows.
Jos (10584)
Jos (10584) 2019 年 5 月 22 日
Ah, my mistake, Adam, I overlooked that in your answer :-)

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


Guillaume
Guillaume 2019 年 5 月 21 日
The question is a bit confusing. Tables are mentioned in the title, but the examples are matrices. Two tables is mentioned in the table, but the example has 3 inputs.
If you were dealing with tables, you'd use outerjoin for what you want:
A = [1 7;
3 15]
B = [2 9;
5 10]
C = [2 5;
3 4]
%cell array of tables
t{1} = array2table(A, 'VariableNames', {'ID', 'A'});
t{2} = array2table(B, 'VariableNames', {'ID', 'B'});
t{3} = array2table(C, 'VariableNames', {'ID', 'C'});
result = outerjoin(t{1}, t{2}, 'MergeKeys', true); %outer join the first two
for tidx = 3:numel(t) %loop over the rest (works with any number of tables)
result = outerjoin(result, t{tidx}, 'MergeKeys', true);
end
For matrices, I'd use Adam's answer.
  1 件のコメント
elmar.a
elmar.a 2019 年 5 月 22 日
There are tables mentioned in the title since that question is so similar to my problem. I'm working with matrices though. Thanks anyways for your answer, it works well for tables.

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

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by