Merge structures with overlapping fieldname

Hello,
I'm trying to intelligently combine to data sets to create a new complete data set. The setup is as follows:
data.set1.ID = [457;568;422;8997;544;366];
data.set1.type = {'fish';'cat';'dog';'horse';'monkey';'yellow'};
data.set2.ID = [568;233;422;8997;366;78887;544;111];
data.set2.num = [68;55;44;22;89446;23447;5975;13544];
I would like the output to be a new structure with 3 fields, ID, type, and num that has been aligned by ID. If there isn't a num or type for a particular ID, it will just have a "NaN" placeholder.
I've tried finding and looping but it was slow and used up all of my RAM (my data set is large). It looks like there is a data type called a table in R2013b but I only have 2013a. I thought there had to be a better way. Thanks for any help you can provide!

回答(2 个)

d.ID=[data.set1.ID;data.set2.ID]
d.type=[data.set1.type;num2cell(repmat(nan,numel(data.set2.ID),1))]
d.num=[repmat(nan,numel(data.set1.ID),1);data.set2.num]
You could do this in R2013a's Statistics Toolbox with the dataset class. If your organization is current on SMS, you can upgrade to the newest release.

类别

帮助中心File Exchange 中查找有关 Structures 的更多信息

提问:

2014-4-15

Community Treasure Hunt

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

Start Hunting!

Translated by