フィルターのクリア

How to find k nearest vectors from a given vector in 3 dimensions ?

4 ビュー (過去 30 日間)
naman
naman 2015 年 5 月 16 日
回答済み: Walter Roberson 2015 年 5 月 16 日
I have a reference vector and I want to find a fixed k nearest neighbors from a matrix ? How can I do it? Is there any direct way?
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 5 月 16 日
When you say "reference vector", do you mean a line defined in N-dimensional space? And you want to find the k points that lie nearest to the line using Euclidean distance? Or does "reference vector" just mean an N-dimensional point and you want to find its k nearest neighbours using a distance measure you have not specified, possibly Euclidean distance?

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2015 年 5 月 16 日
編集済み: Star Strider 2015 年 5 月 16 日
This is one way:
V = randi(50, 1, 3); % Vector
M = randi(50, 15, 3); % Matrix
%
dif = bsxfun(@minus, V, M); % Subtract Vector from Matrix
D = sqrt(sum(dif.^2,2)); % Euclidean Distance Metric
[Ds,Ix] = sort(D,'ascend'); % Sort Ascending
k = 5; % Number Of Neighbours
KNN = M(Ix(1:k),:); % K-th Nearest Neighbours
This simply finds them. If you have the Statistics Toolbox, the pdist2 function is likely more efficient.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 5 月 16 日
If "reference vector" is a line then according to Roger Stafford over here
Let Q1 and Q2 be any two distinct points of the line and P the point in question, then
d = norm(cross(Q2-Q1,P-Q1))/norm(Q2-Q1);
will give the requested orthogonal distance.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by