How to vectorize this operation

1 次查看(过去 30 天)
Pete sherer
Pete sherer 2018-2-3
Hi,
Could you help guide how I can vectorize this operation? I have the original fullset matrix which i want to retain and populate for each of the locID (4 locID total). For each locID, it looks up the loss values from ELT when there's a match on eventid.
fullset= struct;
fullset.yearid= [1 1 2 3 4 5 6 7 7 7]';
fullset.eventid=[201 202 203 204 201 210 208 209 210 210]';
fullset.indx = [1:10]';
fullset= struct2table( fullset);
ELT= struct;
ELT.eventid= [ 201 203 209 210 210 210 201 210]';
ELT.loss = [ 100 100 100 200 1 1 5 1]';
ELT.locid = [ 1 1 1 1 2 3 4 4]';
ELT= struct2table( ELT);
This is the operation I used to come up with full matrix set [10x4]. It has the full sequence from fullset, and for each indx, it's filled in with the loss from ELT of the same eventid
uniqLocID= unique( ELT.locid);
nLoc= length( uniqLocID);
simLocFull= deal( zeros( length( fullset.eventid), nLoc) );
for runi= 1: nLoc
tloc = (ELT.locid== uniqLocID( runi)); % (JlocID== runi);
tdata= struct;
[tdata.eventid, tdata.loss]= deal( ELT.eventid(tloc), ELT.loss(tloc));
[ tlocMaster, locELT]= ismember( fullset.eventid, tdata.eventid);
simLocFull( tlocMaster, runi)= tdata.loss( locELT( tlocMaster));
end % for runi
I tried using outerjoin, but was unsuccessful.
tall=outerjoin( fullset, ELT,'Type','LEFT','MergeKeys', true, 'LeftKeys',{'eventid'},'RightKeys',{'eventid'}, 'LeftVariables',{'yearid','eventid'},'RightVariables',{'locid','loss'});
Thanks in advance.

回答(1 个)

Pete sherer
Pete sherer 2018-2-4
编辑:Pete sherer 2018-2-4
Each locid in the ELT has a unique set of eventid by the way

类别

Help CenterFile Exchange 中查找有关 Database Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by