how to find euclidean distance between training and test data?

8 visualizaciones (últimos 30 días)
I have two matrices A of size 2x5 and B of size 2x2 such that each column is a feature vector. I want to calculate the euclidean distance between A and B. i.e I want to calculate the euclidean distance between first column of B with every column of A and similarly need to calculate the second column of B with every column of A .
A=rand(5,2);
B=rand(2,2);
for i=1:2
for j=1:5
d=sqrt(B(i,:)-A(j,:));
end
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 19 de Oct. de 2018
Statistics toolbox:
d = pdist2(A.', B.');
pdist2 normally operates row by row, which is why the .' are there, to make your column-oriented data into row-oriented data.
  2 comentarios
kitty varghese
kitty varghese el 19 de Oct. de 2018
Im getting an error.
Error using pdist2 (line 138)
X and Y must have the same number of columns.
Walter Roberson
Walter Roberson el 19 de Oct. de 2018
Your problem statement says,
I have two matrices A of size 2x5 and B of size 2x2
and that
each column is a feature vector.
But your sample code has
A=rand(5,2);
which is a 5 x 2 array, not a 2 x 5 array. The columns of your actual A are length 5, and the columns of your actual B are length 2, and it is not meaningful to find the euclidean distance between objects with different lengths.
Your sample code has
sqrt(B(i,:)-A(j,:))
which is comparing all of the columns of particular rows, which would be suitable for the case where the rows are feature vectors, not column vectors.
If your rows are feature vectors then pdist2(A, B)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by