Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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))

Réponses (3)

Guillaume
Guillaume le 24 Fév 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 le 24 Fév 2018
Modifié(e) : javad ebrahimi le 24 Fév 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 commentaires
kmla
kmla le 24 Fév 2018
I want to calculate the similarity between u and v '
Guillaume
Guillaume le 24 Fév 2018
Modifié(e) : Guillaume le 24 Fév 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?

Connectez-vous pour commenter.


Amelia
Amelia le 9 Nov 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.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by