Home > basic, code, matlab > calculating distance matrix; Matlab

calculating distance matrix; Matlab

A simple code to calculate the L2 distance matrix between any features.

UPDATE: found pdist and pdist2 functions in matlab, so no more need of following function

function D = calculateDistMatrix( points1, points2, f)
%D = calculateDistMatrix( points1, points2)
%points1: is m by n matrix, where n is number of points
if nargin ❤
    f = 0;
end
totalPoints1 = size(points1,2);
totalPoints2 = size(points2,2);
if f == 0
D= zeros(totalPoints1, totalPoints2);
else
D = sparse(totalPoints1, totalPoints2);
end
for i=1:totalPoints1
    D(i,:) = sqrt(sum((points2-repmat(points1(:,i), 1,totalPoints2)).^2));
end

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: