The Magic Matrix

Do you believe in the magic of numbers? If not, check out these magic matrices in this post.


The Magic Matrix

The magic matrix is a n-by-n square matrix in which every row, column and diagonal add up to the same number. Note that n is at least 3 as there are no 2 by 2 magic matrices.

Here are a few examples of magic matrices.

An example of a 3 by 3 magic matrix would be

\displaystyle \begin{bmatrix} 2 & 7 & 6 \\ 9 & 5 & 1 \\ 4 & 3 & 8 \end{bmatrix}

The rows each add up 15, the columns each add up to 15 and the main diagonals add up to 15 (2 + 5 + 8 = 15 and 4 + 5 + 5 = 15).

An example of a 4 by 4 magic matrix would be

\displaystyle \begin{bmatrix} 1 & 12 & 8 & 13 \\ 15 & 6 & 10 & 3 \\ 14 & 7 & 11 & 2 \\ 4 & 9 & 5 & 16 \\ \end{bmatrix}

The row sums, column sums and the main diagonal sums (top left to bottom right and bottom left to top right) equal to 34.


Magic Matrices in MATLAB and R

For MATLAB, and R users creating magic matrices is quite easy.

In MATLAB, the command is magic(n) where n \geq 3.

In the statistical program R, the magic package needs to be installed first. After installation, the magic library needs to be summoned (bad pun). The command in R would be like in MATLAB which is magic(n).

Here is an example in R of a 11 by 11 magic matrix.

The sums of each row is 671, the sums of each column is 671 and trace of the matrix is 671.

The featured image is from http://mathworld.wolfram.com/images/eps-gif/MagicSquare_800.gif.