How to find the closest data row index
Show older comments
Input data (sort by time): M:
1.0
1.2
3.1
4.0
1.2
1.0
4.8
given data:
V:1.2
I want to find the most closer one in the input data
I use the below function:
[~,idx] = min(sum(bsxfun(@minus,M,V).^2,2));
M(idx,:)
But here, there two rows (duplicate data) row2 &row5, if this is the case, I want to pick the more recent row index i.e., row5 (but the present code giving row 2).
Accepted Answer
More Answers (1)
EDITED for 2D matrix,
A = abs(M-V);
ind = arrayfun(@(k) find(A(:,k)==min(A(:,k)),1,'last'),1:length(V))
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!