# Measuring correlation between two matrices

I have two matrices A and B of the same dimensions. Each cell a(i,j) represents some parameter I'm interested in, and b(i,j) represents the corresponding value in the other matrix. The cells are not interrelated. What's the best way to represent how close these two matrices are?

The entries in these matrices are the number of occurrences of some event out of a large sample of events. If this is the case, does the problem make more sense?

If the cells are not interrelated then the fact that they are in matrices rather than just equal length columns of numbers doesn't matter, right?

Given they are counts, a suitable way of comparing them is probably the sum of squared differences, weighted by the inverse of the average of the two observations for each comparison cell.

$\sum\frac{(a_{i,j}-b_{i,j})^2}{(a_{i,j}+b_{i,j})/2}$

But what does this tell you about "how close", except that smaller means closer? Turning this statistic into something you can compare to any other number would depend on why you are doing this eg are there a range of candidate Bs trying to look like A.

I would use a suitably-defined inner product: $s_{A,B}=\left< A,B\right>$. If $A$ and $B$ are square, you could use $s=\mathrm {tr} (A B^T)$. If you want the result to be normalized you should divide by the norm.

The entries in these matrices are the number of occurrences of some event out of a large sample of events. If this is the case, does the problem make more sense?

This tells us the entries are non-negative so it does provide information.

• I may not be thinking straight but I'm having trouble figuring out how to interpret $s$. Apr 18 '12 at 0:43
• The inner product is maximized when the two vectors are collinear, and zero when they are perpendicular. The particular example I gave simply meets the requirements of an inner product.
– Emre
Apr 18 '12 at 1:01
• Collinear isn't the same as "close" though. If every element of B were 100 times its equivalent in A that probably wouldn't meet the OP's need (although they probably need to be more precise about what they mean). Apr 18 '12 at 1:39
• I think it would still be useful as is if A and B are snapshots at different times. Then B would be collinear with A assuming a constant rate of events. Normalizing each matrix before taking the inner product would also solve the "problem".
– Emre
Apr 18 '12 at 2:02