Find closest value for each row in timetable
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a timetable with 3 columns.
I would like to use the first column (Var1) as reference to select the closest value in the 2 other columns, resulting in a timetable with just 1 column (i.e. the value closest to Var1).
If both values in the 2 colums are NaN, then the resulting value can be left out.
I tried using a for loop but I'm not sure this is the best way forward.
Does anyone has a suggestion how to do this?
Thanks a lot
0 Commenti
Risposta accettata
Star Strider
il 9 Gen 2023
I am defining ‘closest’ as the minimum absolute difference between ‘Var1’ and the last two variables.
(My attempt at a more direct approach for the creation of ‘Var4’ failed, so I went with the loop, that worked.)
Try something like this —
LD = open(websave('nHS_TS','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1257052/nHS_TS.mat'));
nHs_TS = LD.nHs_TS
[~,idx] = min(abs(nHs_TS{:,1}-[nHs_TS{:,[2 3]}]),[],2,'omitnan'); % Minimum Absolute Difference
Extract = nHs_TS{:,[2 3]};
for k = 1:numel(idx)
Var4(k,:) = Extract(k,idx(k));
end
nHs_TS.NewVar= Var4
.
2 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Logical 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!