Find closest value for each row in timetable
6 次查看(过去 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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!