MATLAB: Follow up: How to merge two different tables using the first column in common

join;MATLABmerge

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.

Best Answer

% 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)