Efficient ways to append new data in Matlab (with example code)

前端 未结 1 1716
梦如初夏
梦如初夏 2020-12-21 09:39

I am looking for methods, built-in functions, good practices... to append new data to a matrix - when the rows and columns are not the same

The data

1条回答
  •  时光说笑
    2020-12-21 09:56

    Here is a solution using unique and indexing:

    %combine the data and take unique value of them + their index
    [C.id,~,date_i] = unique([A.dates(:);B.dates(:)]);
    [C.dates,~,id_i] = unique([A.id B.id]);
    
    C.values = nan(numel(C.dates),numel(C.id));
    %use matrix indexing to fill the sub-materices corresponding to elements of A and B
    C.values(date_i(1:numel(A.dates)),id_i(1:numel(A.id)))=A.values;
    C.values(date_i(numel(A.dates)+1:end),id_i(numel(A.id)+1:end))=B.values;
    

    0 讨论(0)
提交回复
热议问题