How can I measure the mean of the Euclidean distances to each point in a scatterplot?

8 views (last 30 days)
Hi everyone!
I'm new in the Matlab word and I need some help to do some of my result (master student).
I have a scatter plot, and I need to measure the mean of the Euclidean distances to each point to the nearest neighbor in the bi-plot space.I found some information about the knnsearch function, but I realy don't know how to use it.
Thanks

Answers (2)

Image Analyst
Image Analyst on 26 Jan 2016
If you have the Stats Toolbox, you can use pdist2(), min(), and mean():
x = rand(10,1);
y = rand(length(x), 1);
plot(x, y, 'b*');
grid on;
% Get distances of each point to every other point.
D = pdist2(x, y)
% Find the min distance for each point
% That is the distance to the closest neighbor.
minDistances = min(D, 2)
% Get the mean of those closest distances
meanClosestDistance = mean(minDistances)
If you don't have that toolbox, you can easily calculate D with a double for loop.

Star Strider
Star Strider on 26 Jan 2016
The ‘knn’ functions are used for classification. If you just want the distances, use the pdist funciton, and if you want to know what the distances are between various points, use the squareform function to get the matrix.
A = randi(99, 15, 2); % Create Data (15x2) Matrix
D = pdist(A, 'euclidean'); % Calculate Distances
S = squareform(D); % Matrix Depiction (Optional)

Community Treasure Hunt

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

Start Hunting!