# How to denote element-wise difference of two matrices

Ok so this may seem like a basic question but I'm getting confused by the mathematical notation.

I am calculating the MSE (mean squared error) between two matrices. I know how to compute this, but do not know how to denote it.

For calculating the MSE, you have to subtract every element of matrix 2 from every element of matrix 1. It's how to denote "subtract element [i,j] of matrix 2 from element [i,j] of matrix 1" that I can't figure out ...

• For two matrices $A$ and $B$, $A-B$ is usually interpreted as elementwise subtraction. See en.wikipedia.org/wiki/Matrix_addition . But what on earth is "the MSE between to matrices"? Commented May 14, 2012 at 14:41
• @MånsT See en.wikipedia.org/wiki/Mean_squared_error for the MSE. I couldn't just write A-B because I wanted to square the difference between each element. Commented May 14, 2012 at 14:51
• My (implicit) point was that the MSE usually is defined as a property of an estimator rather than some sort of measure between matrices. Judging from Erik's answer however, I take it that it was the sum of squared differences that you were looking for. Commented May 14, 2012 at 15:06

Assume your matrices are called $A$ and $B$, then it is usual to notate their elements with $a_{ij}$ respectively $b_{ij}$. So you could denote the sum of the squared errors as

$$\text{SSE} = \sum_{i,j} (a_{ij}-b_{ij})^2.$$

You would get your MSE in the usual way, by taking the average. Does this answer your question? It sorts of seems to sample. You could also first define a new matrix $C$, via

$$c_{ij} = a_{ij}-b_{ij}$$

and work with that. As per the comment above, for the whole matrix you can also just write $$C=A-B$$

which works out elementwise as given above.

• The first answer formula for SSE was just what I needed, thanks. Commented May 14, 2012 at 14:50
• +1 A standard way, in matrix notation, to express this $\text{SSE}$ is $\operatorname{Tr}((\mathbf{A-B})^\prime(\mathbf{A-B}))$.
– whuber
Commented May 14, 2012 at 15:42

Standard notation for addition/subtraction of matrices refers to elementwise addition/subtraction, so with standard notation you have:

$$\mathbf{A}-\mathbf{B} = \begin{bmatrix} a_{11} - b_{11} & a_{12} - b_{12} & \cdots & a_{1m} - b_{1m} \\ a_{21} - b_{21} & a_{22} - b_{22} & \cdots & a_{2m} - b_{2m} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} - b_{n1} & a_{n2} - b_{n2} & \cdots & a_{nm} - b_{nm} \\ \end{bmatrix}.$$

The quantity of interest to you (if I understand your description correctly) can be written in matrix form as:

\begin{align} \text{SSE} &\equiv \sum_{i=1}^n \sum_{j=1}^m (a_{ij} - b_{ij})^2 \\[6pt] &= \sum_{i=1}^n \sum_{j=1}^m ([\mathbf{A}-\mathbf{B}]_{ij} )^2 \\[6pt] &= \sum_{i=1}^n \sum_{j=1}^m [\mathbf{A}-\mathbf{B}]_{ij} [\mathbf{A}-\mathbf{B}]_{ij} \\[6pt] &= \sum_{i=1}^n \sum_{j=1}^m [(\mathbf{A}-\mathbf{B})^\text{T}]_{ji} [(\mathbf{A}-\mathbf{B})]_{ij} \\[6pt] &= \sum_{j=1}^m \sum_{i=1}^n [(\mathbf{A}-\mathbf{B})^\text{T}]_{ji} [(\mathbf{A}-\mathbf{B})]_{ij} \\[6pt] &= \sum_{j=1}^m [(\mathbf{A}-\mathbf{B})^\text{T} (\mathbf{A}-\mathbf{B})]_{jj} \\[10pt] &= \text{tr}((\mathbf{A}-\mathbf{B})^\text{T} (\mathbf{A}-\mathbf{B})). \\[6pt] \end{align}