Given a vector, how to pair them by nearest?
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have a vector with values. I have to pair them by nearest.
Is there any magic command to do it?
Thanks!
0 个评论
采纳的回答
Sean de Wolski
2013-2-25
编辑:Sean de Wolski
2013-2-25
doc knnsearch
If you have the Statistics Toolbox this sounds like your magic command.
Of course, if you just need a magic command, you can always run:
magic(5)
More Using knnsearch here is probably overkill when you could just use a for-loop, finding the minimum of the exclusive set. But here is how I would do this with knnsearch:
[idx,D] = knnsearch(values',values','K',2) %Get two because the first will be the value itself
idx(:,2) %2nd column is the closest one that isn't the same
D(:,2) %Distance
values(idx(:,2)) %What was the original value?
7 个评论
Teja Muppirala
2013-2-25
Assuming an even number of elements, isn't the answer just SORTing the vector and taking two at a time?
values = [1.5 0.6 1.9 0.4 1.3 0.1];
reshape(sort(values),2,[])'
更多回答(1 个)
Jos (10584)
2013-2-25
What do you mean by ' pair them by nearest '? Do you want to SORT the values?
Otherwise, can you give a small example of your vector and the required result of such a magic function?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Statistics and Machine Learning Toolbox 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!