Cosine similarity between two matrices

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!

 Accepted Answer

I tend to prefer pdist2,
UV=[U(:),V(:)];
D=1-pdist2(UV,UV,'cosine')

5 Comments

Thank you for your answer!
If I have already two matrices A and B, each of them containing UV, can I use directly:
D=1-pdist2(A,B,'cosine')
Can I ask why "1-"?
Since I am interested in the similarity between the whole two matrices, can I obtain just one value of similarity?
Matt J
Matt J on 25 May 2022
Edited: Matt J on 25 May 2022
If I have already two matrices A and B, each of them containing UV, can I use directly:
If A and B are Nx2 and A=B, then yes. Otherwise, it is not equivalent to what I've presented to you.
Can I ask why "1-"?
Because pdist and pdist2 don't compute cosSim. They compute 1-cosSim.
Since I am interested in the similarity between the whole two matrices, can I obtain just one value of similarity?
I don't know anymore what "two vectors" we are talking about. In your original post, you wrote that you had 16 vectors in a quiver plot and wanted to calculate a similarity between each pair of those vectors. Because there are 16^2 pairs, that should give you 16^2 similarity measurements. With my solution, you should be finding that UV is a 16x2 matrix whose rows are the different vectors, and that pdist2 is giving you a 16x16 matrix, containing those 16^2 similarities.
Thanks again for your answer. If you look at the plots of the two quivers, the arrows form a particular pattern, based on the matrices they derive from. The final goal is to calculate the similarity value between the two plots, not of the single "couple of arrows". When I use "cosSim = dot(a,b)/(norm(a)*norm(b));", for example, where a and b are each a 32x1 vectors, I obtain one value. So I wonder how can I obtain the same result using pdist2.
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')

Sign in to comment.

More Answers (0)

Categories

Asked:

on 24 May 2022

Commented:

on 30 May 2022

Community Treasure Hunt

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

Start Hunting!