How can i create a matrix with samples from irregular X,Y

Example:
Am01= (x;y) = (1 2 3 4 5 ; 10 20 30 40 50)
Am02= (x;y) = (1 3 4 ; 30 20 50)
Am03= (x;y) = (1 2 5 ; 25 93 47)
Matrix Funded:
x - y1 - y2 - y3
1 10 30 25
2 20 0 93
3 30 20 0
4 40 0 0
5 50 50 47

 Accepted Answer

One possible solution would be like this:
Am01 = [1 2 3 4 5 ; 10 20 30 40 50]';
Am02 = [1 3 4 ; 30 20 50]';
Am03 = [1 2 5 ; 25 93 47]';
x = unique([Am01(:,1) ; Am02(:,1) ; Am03(:,1)]);
A = zeros(numel(x),4);
A(:,1) = x;
[~,loc] = ismember(Am01(:,1),A(:,1));
A(loc,2) = Am01(:,2);
[~,loc] = ismember(Am02(:,1),A(:,1));
A(loc,3) = Am02(:,2);
[~,loc] = ismember(Am03(:,1),A(:,1));
A(loc,4) = Am03(:,2);
The output is:
>> A
A =
1 10 30 25
2 20 0 93
3 30 20 0
4 40 50 0
5 50 0 47

1 Comment

+1
If your data is stored in table variables, you can use outerjoin function. Here is an example.
Am01 = [1 2 3 4 5 ; 10 20 30 40 50]';
Am02 = [1 3 4 ; 30 20 50]';
Am03 = [1 2 5 ; 25 93 47]';
T1 = array2table(Am01,'VariableNames',{'ID','Am01'});
T2 = array2table(Am02,'VariableNames',{'ID','Am02'});
T3 = array2table(Am03,'VariableNames',{'ID','Am03'});
T = outerjoin(T1,T2,'Keys','ID','MergeKeys',true);
T = outerjoin(T,T3, 'Keys','ID','MergeKeys',true);
T = fillmissing(T,'constant',0);
Result:
>> T
ans =
5×4 table
ID Am01 Am02 Am03
__ ____ ____ ____
1 10 30 25
2 20 0 93
3 30 20 0
4 40 50 0
5 50 0 47

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!