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

조회 수: 1 (최근 30일)
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))

답변 (3개)

Guillaume
Guillaume 2018년 2월 24일
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 2018년 2월 24일
편집: javad ebrahimi 2018년 2월 24일
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
Guillaume
Guillaume 2018년 2월 24일
편집: Guillaume 2018년 2월 24일
@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?

댓글을 달려면 로그인하십시오.


Amelia
Amelia 2019년 11월 9일
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.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by