73

I'm in the process of writing an application which identifies the closest matrix from a set of square matrices $M$ to a given square matrix $A$. The closest can be defined as the most similar.

I think finding the distance between two given matrices is a fair approach since the smallest Euclidean distance is used to identify the closeness of vectors.

I found that the distance between two matrices ($A,B$) could be calculated using the Frobenius distance $F$:

$$F_{A,B} = \sqrt{trace((A-B)*(A-B)')} $$

where $B'$ represents the conjugate transpose of B.

I have the following points I need to clarify

  • Is the distance between matrices a fair measure of similarity?
  • If distance is used, is Frobenius distance a fair measure for this problem? any other suggestions?
bubba
  • 44,617
Synex
  • 995
  • 4
    There are many ways to measure the "distance" between two matrices (just as there are many ways to measure the distance between two vectors). Without some more information, it's impossible to say which one is best for you. – bubba Sep 28 '13 at 12:40
  • @bubba I just want to find the closest matrix to a give matrix numerically. I'm creating a closest match retriever for a given matrix. If you can let me know the other possible methods you know for distance measures that would be a great help. – Synex Sep 28 '13 at 15:51
  • 3
    Provided that you "just want to compute something", a good norm is the norm for which the problem can be easily solved. This is often the case for the Frobenius norm. – Algebraic Pavel Sep 29 '13 at 11:46

3 Answers3

65

Some suggestions. Too long for a comment:

As I said, there are many ways to measure the "distance" between two matrices. If the matrices are $\mathbf{A} = (a_{ij})$ and $\mathbf{B} = (b_{ij})$, then some examples are: $$ d_1(\mathbf{A}, \mathbf{B}) = \sum_{i=1}^n \sum_{j=1}^n |a_{ij} - b_{ij}| $$ $$ d_2(\mathbf{A}, \mathbf{B}) = \sqrt{\sum_{i=1}^n \sum_{j=1}^n (a_{ij} - b_{ij})^2} $$ $$ d_\infty(\mathbf{A}, \mathbf{B}) = \max_{1 \le i \le n}\max_{1 \le j \le n} |a_{ij} - b_{ij}| $$ $$ d_m(\mathbf{A}, \mathbf{B}) = \max\{ \|(\mathbf{A} - \mathbf{B})\mathbf{x}\| : \mathbf{x} \in \mathbb{R}^n, \|\mathbf{x}\| = 1 \} $$ I'm sure there are many others. If you look up "matrix norms", you'll find lots of material. And if $\|\;\|$ is any matrix norm, then $\| \mathbf{A} - \mathbf{B}\|$ gives you a measure of the "distance" between two matrices $\mathbf{A}$ and $\mathbf{B}$.

Or, you could simply count the number of positions where $|a_{ij} - b_{ij}|$ is larger than some threshold number. This doesn't have all the nice properties of a distance derived from a norm, but it still might be suitable for your needs.

These distance measures all have somewhat different properties. For example, the third one shown above will tell you that two matrices are far apart even if all their entries are the same except for a large difference in one position.

bubba
  • 44,617
  • 1
    Do all of these satisfy the conditions of a metric space? – eric Aug 17 '18 at 15:32
  • 2
    The ones for which I gave formulas are distances derived from matrix norms, so they are distances in the metric space sense. The last one (counting large differences) is not a metric. – bubba Aug 18 '18 at 02:28
  • Can you please provide the references for these formulas? – Shayan Mar 27 '22 at 12:11
  • As I said, look up “matrix norms” – bubba Mar 27 '22 at 22:31
  • OP mentioned square matrices, so if they have the additional property of being covariance matrices, then I suggest looking into: W. Förstner and B. Moonen, A Metric for Covariance Matrices, 1999 (and 2003). – Number Dec 30 '23 at 00:44
10

If we have two matrices $A,B$. Distance between $A$ and $B$ can be calculated using Singular values or $2$ norms.

You may use Distance $= \vert(\text{fnorm}(A)-\text{fnorm}(B))\vert$ where fnorm = sq root of sum of squares of all singular values.

Censi LI
  • 6,075
Mohaar
  • 101
  • 1
    Can you expand some on this answer? What are the properties of this distance / where did you get it from? – alexpghayes Oct 06 '20 at 22:13
  • I think this is not always correct, you can find two matrices that are quite different but they have the same singular values https://math.stackexchange.com/questions/4191889/find-matrix-with-same-singular-values-svd – Jonathan1234 Jul 07 '21 at 10:11
5

A possible measure of similarity between matrices is: $$ D(AB)=\sum_{i,j}A_{ij}\left[\log\frac{A_{ij}}{B_{ij}} + \log B - \log A\right], \text{ where } A=\sum_{i,j}A_{ij}, B=\sum_{i,j}B_{ij}. $$ This measure is inspired by Kullback-Leibler divergence for probability vectors $$ p_{ij}=\frac{A_{ij}}{A},q_{ij}=\frac{B_{ij}}{B}. $$ Note that this is not a distance, as it is not symmetric in respect to $A$ and $B$, however, it could be symmetrized, e.g., as Jensen-Shannon divergence. It is quite useful as is, e.g., when one measures divergence from the ideal/exact result.

Roger V.
  • 763
  • Shouldn't it be $D=\sum_{ij}p_{ij}\log\left(p_{ij}/q_{ij}\right)$ to remind the Kullback-Leibler divergence ? Said differently, a $1/A$ prefactor is missing in your answer ... – FraSchelle Jan 14 '25 at 08:57
  • @FraSchelle Indeed, to be exactly KL divergence, it would need a prefactor. However, if matrrix $A_{ij}$ is fixed, it is just a matter of scale/units. – Roger V. Jan 14 '25 at 09:09