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

可紊 提交于 2019-11-29 15:15:42

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!