Cosine similarity between two matrices

152 ビュー (過去 30 日間)
Paola
Paola 2022 年 5 月 24 日
コメント済み: Paola 2022 年 5 月 30 日
Dear all,
I have some vectors 32x1, representing force fields. I use the quiver function to plot and visualize the fields. (see pic attached) Obtaining a quiver showing 4x4 vectors, for a total of 16 vectors.
X = [1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4];
Y = [1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4];
U = W(1:2:31);
V = W(2:2:32);
figure, quiver(X,Y,U',V');
Even if visually they look very similar, I need to calculate a cosine similarity value, between the different vectors.
Checking online I found that this formula:
cosSim = dot(a,b)/(norm(a)*norm(b));
and also the function
D = pdist(X,'cosine').
I wonder if these two functions can be applied to my case and which one is better to obtain just a similarity value for each couple of vector compared.
Thank you!

採用された回答

Matt J
Matt J 2022 年 5 月 24 日
I tend to prefer pdist2,
UV=[U(:),V(:)];
D=1-pdist2(UV,UV,'cosine')
  5 件のコメント
Matt J
Matt J 2022 年 5 月 25 日
編集済み: Matt J 2022 年 5 月 25 日
There is no advantage to pdist2 if you are only comparing two vectors, a and b. You can just use your cosSim formula directly. However, it could hypothetically be done with,
1-pdist2(a(:).',b(:).','cosine')
Paola
Paola 2022 年 5 月 30 日
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeVector Fields についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by