number of nearest neighbous around each element of a matrix

1 回表示 (過去 30 日間)
jack
jack 2014 年 5 月 18 日
コメント済み: jack 2014 年 5 月 19 日
I have a matrix that contain coordinates I=[x y z]. I need to find the number of nearest neighbors for each point in the matrix within a radius R. Thanks a lot!
Jack,

採用された回答

Image Analyst
Image Analyst 2014 年 5 月 18 日
How about just a brute force approach (though slightly vectorized):
% Set up / initialize.
numberOfCoordinates = 100;
m = rand(numberOfCoordinates, 3); % Don't us I because I is a bad name.
radius = 0.5;
% Check each point for others within radius.
for row = 1 : numberOfCoordinates
% Get the distance of every point to this point.
distances = sqrt((m(row,1) - m(:, 1)).^2 + ...
(m(row, 2) - m(:, 2)).^2 + ...
(m(row, 3) - m(:, 3)).^2);
% Count the number of points that are
% greater than 0 (to exclude this point itself)
% but less than or equal to the radius.
counts(row) = sum(distances > 0 & distances <= radius);
end
% Report to the command line the counts:
counts
  1 件のコメント
jack
jack 2014 年 5 月 19 日
Thank you! This works great.

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

その他の回答 (1 件)

Youssef  Khmou
Youssef Khmou 2014 年 5 月 18 日
編集済み: Youssef Khmou 2014 年 5 月 18 日
This problem can be solved using the equation of radius in three dimensional space such as if x²+y²+z² < R² then the point is inside the sphere, let us elaborate an example :
M=randn(300,3);
R=0.5;
for n=1:300
if sqrt((M(n,1)^2)+(M(n,2)^2)+(M(n,3)^2))<= R
C(n,:)=M(n,:);
end
end

カテゴリ

Help Center および File ExchangeStatistics and Machine Learning Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by