How to do inline function with unique instead of using for loop
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
tdata = table([1 1 1 3 4 4 4 4]', ["eq","fr","wt","fl","eq","fr","fl","tr"]', [24 55 10 5 3 5 7 9 ]', int64([ 5 2 7 1 50 10 5 5 ]'),...
'VariableNames',["EBID","Peril","Loss","Trial"]);
refGrp = [ "EBID"];
[ uniqEBID, ~, JGrp]= unique( tdata( :, refGrp));
uniqEBID.maxLoss = accumarray( JGrp, tdata.Loss, [], @max);
instead of doing for loop below, can we use inline function with accumarray to do task below. Basically selecting other fields that has same index as maxLoss
for runi= 1: height( uniqEBID_Type)
tloc = (JGrp== runi);
tdata_i = tdata( tloc, :);
[ tmax, idx]= max( tdata_i.Loss);
uniqEBID.Peril( runi) = tdata_i.Peril( idx);
uniqEBID.Trial( runi) = tdata_i.Trial( idx);
end
0 Commenti
Risposta accettata
Matt J
il 17 Mag 2025
Modificato: Matt J
il 18 Mag 2025
If by "inline function", you mean an anonymous function, then no. But you can certainly do it with a local function.
tdata = table([1 1 1 3 4 4 4 4]', ["eq","fr","wt","fl","eq","fr","fl","tr"]', [24 55 10 5 3 5 7 9 ]', int64([ 5 2 7 1 50 10 5 5 ]'),...
'VariableNames',["EBID","Peril","Loss","Trial"]);
refGrp = [ "EBID"];
uniqEBID=splitapply(@aggregate, tdata, findgroups(tdata.(refGrp)) )
function out=aggregate(EBID,Peril,Loss,Trial)
[maxLoss,idx]=max(Loss);
out=table(EBID(idx),Peril(idx), maxLoss,Trial(idx), ...
'Var',{'EBID','Peril', 'maxLoss','Trial'});
end
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Function Creation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!