How do we calculate Euclidean Distance Matrix in Matlab ?
96 次查看(过去 30 天)
显示 更早的评论
Dear matlab Experiences
I have (A) matrix where rows instances and columns features, such as A=[1,0,0.21,...etc];
I would like to calculate Distance matrix for A, when i browsed matlab functions and question i have found so many answers but i don't know which one satisfy Euclidean distance matrix ?
both codes give a distance matrix, can please some one give an explanation about second code? and is matlab support another distance matrix like : squared Euclidean distance, dot product, edit distance, manhaten?
functions are:
Distancematrix = dist(A);
and
distmatrix = squeeze(sqrt(sum(bsxfun(@minus,A,reshape(A',1,size(A,2),size(A,1))).^2,2)))
thanks
0 个评论
采纳的回答
更多回答(1 个)
Image Analyst
2017-1-15
There is a Euclidean Distance function in the Image Processing Toolbox, but I don't think you want that since it works only with binary data. Maybe you want pdist2(). You can also use pdist, though it's a little more complicated, and I attach a demo for that.
% Demo to demonstrate how pdist() can find distances between all points of 2 sets of points.
% Requires the Statistics and Machine Learning Toolbox because of the pdist() and squareform() functions.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 28;
numPoints1 = 5;
numPoints2 = 3;
points1 = [ones(numPoints1,1), (41:(41+numPoints1-1))'];
points2 = [2*ones(numPoints2,1), (41:(41+numPoints2-1))'];
% Plot all the points
subplot(2, 2, 1);
plot(points1(:, 1), points1(:, 2), 'bo', 'MarkerSize', 12, 'LineWidth', 1.5);
hold on;
plot(points2(:, 1), points2(:, 2), 'bs', 'MarkerSize', 12, 'LineWidth', 1.5);
xlim([0, 3]);
grid on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('All Points', 'FontSize', fontSize);
legend('Set #1', 'Set #2')
text(.6, 43.25, 'Point Set #1', 'FontSize', 13, 'Color', 'b');
text(2.1, 43.25, 'Point Set #2', 'FontSize', 13, 'Color', 'b');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Get the distance from every point to every other point.
pDistances = pdist([points1;points2])
% That's hard to interpret though, so
% let's reshape that into a nice table
sfd = squareform(pDistances)
% Extract a table where the row index is the index of point 1,
% and the column index is the index of point 2
distances = sfd(1:numPoints1, numPoints1+1:end)
% No semicolons above so results will be reported in the command window.
% Plot all the lines between points
for p2 = 1 : numPoints2
x2 = points2(p2, 1);
y2 = points2(p2, 2);
subplot(2, 2, p2+1); % Do it in a new plot
for p1 = 1 : numPoints1
x1 = points1(p1, 1);
y1 = points1(p1, 2);
plot([x1, x2], [y1, y2], 'bo-', 'MarkerSize', 9, 'LineWidth', 1.5);
hold on;
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Label the line with the length that it is.
lineLabel = sprintf('Distance = %.3f.', distances(p1, p2));
text(0.3, points1(p1, 2), lineLabel, 'FontSize', 13);
pointLabel = sprintf('Point %d of Point Set #2', p2);
text(x2+ 0.1, y2, pointLabel, 'FontSize', 13, 'Color', 'b');
end
% Fancy up the plot
caption = sprintf('Distances to point #%d of Point Set #2', p2);
title(caption, 'FontSize', fontSize);
xlim([0, 3]);
ylim([40, 46]);
grid on;
end
message = sprintf('Done with demo.\nCheck out command window for a table of distances');
helpdlg(message);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!