Newbie: Euclidean distance of a matrix??

hello all, i am new to use matlab so guys i need ur help in this regards. if i have a mxn matrix e.g
X=[5 3 1; 2 5 6; 1 3 2]
i would like to compute the distance matrix for this given matrix as
D= [d11 d12 d13; d21 d22 d23; d31 d32 d33]
where d(ij)= euclidean distance between row i and j.
in my thinking i applied a for loop like this
% for r=1:rows
% for c=1:cols
% for k=1:3
% d(r,c)= sqrt(sum(a(r,k)-a(c,k)).^2);
% end
% end
% end
but this thing doen't gives the desired result. can some one please correct me and also it would b nice if it would be not only for 3x3 matrix but for any mxn matrix..
thanks alot in advance i wish a nice smilling day

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 23 de Abr. de 2012
Following your rules:
X=[5 3 1; 2 5 6; 1 3 2]; %sample matrix
myEdist = squeeze(sqrt(sum(bsxfun(@minus,X,reshape(X',1,size(X,2),size(X,1))).^2,2))) %Engine
Take the difference of elements in each combination of rows. Square it, sum it, root it, and squeeze it!.

1 comentario

Usman  Ali
Usman Ali el 25 de Abr. de 2012
that works absolutly perfect. but i have another matrix of size 10x6, applied your code and found that the ans came out to be squared root of the original Ans to be expected.
so i changed a little bit of ur code by removing sqrt(). or by squaring myEdist and then the ans was perfectly matching.
i.e
myEdist = squeeze(sum(bsxfun(@minus,X,reshape(X',1,size(X,2),size(X,1))).^2,2))
or
myEdist = squeeze(sqrt(sum(bsxfun(@minus,X,reshape(X',1,size(X,2),size(X,1))).^2,2))).^2
but when i applied the new edited code back to old 3x3 matrix. the ans was the squared of the expected ans. so i have to take again the sqrt(ans).
would it b possible to think not only for the square matrices but of any size MxN

Iniciar sesión para comentar.

Más respuestas (1)

Thomas
Thomas el 23 de Abr. de 2012
Dunno if this is what you need:
X=[5 3 1; 2 5 6; 1 3 2]
for i=1:length(X)
dist(i,:)=pdist(X(:,i),'euclidean')';
end
dist
EDIT
All you need is
X=[5 3 1; 2 5 6; 1 3 2]
pdist(X)
or if you need a 3x3 matrix of output:
for i=1:length(X)
Y=circshift(X,i+1);
q(i,:)=pdist(Y);
q(i,i)=0;
end
q
Using Teja's comment might be the easiest:
X=[5 3 1; 2 5 6; 1 3 2]
squareform(pdist(X));

2 comentarios

Usman  Ali
Usman Ali el 23 de Abr. de 2012
unfortunatlly thats also incorrect.
actually any distance e.g d12 could be found as
1st row minus 2nd row, square each value, add all the values in a row vector and then take the sqrt i.e
1. sum =(x(1,1)-x(2,1)).^2 + (x(1,2)-x(2,2)).^2 + (x(1,3)-x(2,3)).^2
2. d12= sqrt(sum)
so in above example the ans should be d12=d21= 6.1644
Teja Muppirala
Teja Muppirala el 23 de Abr. de 2012
X=[5 3 1; 2 5 6; 1 3 2]
squareform(pdist(X))
will convert it into the 3x3 matrix

Iniciar sesión para comentar.

Categorías

Más información sobre Operators and Elementary Operations en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by