I'm trying to find the similarity between two 4D matrices. Because cosine similarity takes the dot product of the input matrices, the result is inevitably a matrix. Is there a way to get a scalar value instead? Could inner product used instead of dot product? That is, is

cossim(A,B) = inner(A,B) / (norm(A) * norm(B))

valid? Or is there a better way to find the similarity between multidimensional matrices?

  • $\begingroup$ Well, this depends very much on how the similarity is defined: there is no general definition thereof: what is the question you are trying to answer? When would you want two matrices to be "close" to each other? $\endgroup$
    – gented
    Jul 12, 2018 at 17:10

1 Answer 1


Don't hack. Do the math instead.

You could reshape your matrix into a vector, then use cosine.

But whether that is sensible to do: ask yourself.

You could also ignore the matrix and always return 0. That is a proper similarity, too. Just usually not useful.

Don't just use some function because you heard the name. You need to understand it's properties, and you need to check whether these properties are helpful for your problem, or not. Choose one because math says this is solving your problem; don't replace the math with random guessing.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.