FUNCTION REFERENCE
magic
Magic square.
Magic square.
Syntax
M = magic(n)Rows, columns and both diagonals of M sum to n*(n^2+1)/2 for n = 1 and n >= 3.
Odd n uses the de la Loubere (Siamese) method, n divisible by 4 the diagonal-complement
method, and the other even n Strachey's quadrant-swap method (Andrews 1917; Rouse Ball 1939).
magic(2) is [1 3; 4 2] as in MATLAB; n below 1 gives []. Non-integer n is floored.
Example
magic(4) % [16 2 3 13; 5 11 10 8; 9 7 6 12; 4 14 15 1]Differences from MATLAB: n is limited by the 16-million-element matrix limit.
These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.