how to calculate the similarity between row vector and column vector using Euclidean distance

1 visualización (últimos 30 días)
how to calculate the similarity between row vector and column vector using Euclidean distance. I tried this code but it gives me this error. Error using - Matrix dimensions must agree.
u=[58 10 0 0 0 0 0 0 0 0 4 11 44 33];
v=[73 45 0 0 0 0 6 6 21 8 26 1 16 47];
t=v';
sim = sqrt(sum((u-t).^2,2))

Respuestas (3)

Guillaume
Guillaume el 24 de Feb. de 2018
If you get this error, that would be because you're using a version of matlab older than R2016b. For versions that do not have the implicit expansion introduced in R2016b, you have to use bsxfun
sim = sqrt(sum(bsxfun(@minus, u, t) .^ 2, 2));

javad ebrahimi
javad ebrahimi el 24 de Feb. de 2018
Editada: javad ebrahimi el 24 de Feb. de 2018
Matlab can't calclate Subtraction of two matrices that do not have the same row and column And the correct way of writing code for the Euclidean distance is as follows:
u=[58 10 0 0 0 0 0 0 0 0 4 11 44 33];
v=[73 45 0 0 0 0 6 6 21 8 26 1 16 47];
sim = sqrt(sum((u-v).^2))
  2 comentarios
Guillaume
Guillaume el 24 de Feb. de 2018
Editada: Guillaume el 24 de Feb. de 2018
@javad,
"Matlab can't calclate Subtraction of two matrices that do not have the same row and column"
Since R2016b, yes it can, as long as the sizes are compatible.
What kmla wanted was the difference of the cartesian product of u and v, which is done exactly how he wrote it in R2016b or earlier. u-v.' will result in matrix of size numel(v) x numel(u).
In earlier versions of matlab, the same result is obtained using bsxfun.
@kmla,
I've told you how to fix your problem in my answer. What else do you need?

Iniciar sesión para comentar.


Amelia
Amelia el 9 de Nov. de 2019
I have the same error as kmla, the difference is that i compare two matrices of the same dimension, my malab is R2018a.
i have anther question, can i compare a cell vector by a matrix using this method, if not how i can do this.
Thanks In advance.

Categorías

Más información sobre Matrix Indexing 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