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

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

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 .

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
install.packages("magic") library(magic) magic(11) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [1,] 54 41 28 15 2 121 108 95 82 69 56 [2,] 42 29 16 3 111 109 96 83 70 57 55 [3,] 30 17 4 112 110 97 84 71 58 45 43 [4,] 18 5 113 100 98 85 72 59 46 44 31 [5,] 6 114 101 99 86 73 60 47 34 32 19 [6,] 115 102 89 87 74 61 48 35 33 20 7 [7,] 103 90 88 75 62 49 36 23 21 8 116 [8,] 91 78 76 63 50 37 24 22 9 117 104 [9,] 79 77 64 51 38 25 12 10 118 105 92 [10,] 67 65 52 39 26 13 11 119 106 93 80 [11,] 66 53 40 27 14 1 120 107 94 81 68 rowSums(magic(11)) [1] 671 671 671 671 671 671 671 671 671 671 671 colSums(magic(11)) [1] 671 671 671 671 671 671 671 671 671 671 671 sum(diag(magic(11))) [1] 671 |

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.