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!