Find closest value for each row in timetable
4 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Star Strider
2023 年 1 月 9 日
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
.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!