How to find the second closest number?
3 次查看(过去 30 天)
显示 更早的评论
I have a matrix looks like this:
A = [1990;1993;1998;2001;2004 ]
And I would like to find the closest value to f=1999 (in this case the first closest value is certainly 1998), but how can I find the second closest value (2001) after I found the first value.
Thanks before
1 个评论
David Goodmanson
2017-1-5
Hi Yuli, if you take abs(A-your_number) and sort it, you will get the entire list of distances. The sort command also has a second output that will show you the location of each one in the unsorted array.
采纳的回答
KSSV
2017-1-5
clc; clear all ;
A = [1990;1993;1998;2001;2004 ] ;
f = 1999 ;
[val,idx] = sort(abs(f-A)) ;
A(idx)
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!