Find closest value for each row in timetable
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Siegmund
am 9 Jan. 2023
Kommentiert: Star Strider
am 9 Jan. 2023
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 Kommentare
Akzeptierte Antwort
Star Strider
am 9 Jan. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!