M = magic(n)
M = magic(n), for n >= 3, is an n-by-n matrix constructed from the integers 1 through n^2 with equal row and column sums. A magic square, scaled by its magic sum, is doubly stochastic.
This is called a magic square because the sum of the elements in each column is the same.M = magic(3)M =8 1 63 5 74 9 2
And the sum of the elements in each row, which can be obtained by transposing twice, is the same.sum(M) =15 15 15
This is also a special magic square because the diagonal elements have the same sum.sum(M')' =151515
The value of the characteristic sum for a magic square of ordersum(diag(M))=15
n is
sum(1:n^2)/n
which, when n = 3, is 15.
n, one for even n not divisible by four, and one for even n divisible by four. The following demonstration makes this apparent:
for n = 3:20A = magic(n);plot(A,'-')r(n) = rank(A);endr
ones,rand
(c) Copyright 1994 by The MathWorks, Inc.