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))
0 个评论
回答(3 个)
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));
0 个评论
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
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"
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!