how to find euclidean distance between training and test data?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
kitty varghese
el 19 de Oct. de 2018
Comentada: Walter Roberson
el 19 de Oct. de 2018
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
0 comentarios
Respuesta aceptada
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
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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Statistics and Machine Learning Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!