Back to Core Functions
magic
Magic square
Syntax
M = magic(n)
Description
Creates an n×n magic square matrix where the sum of each row, column, and both main diagonals equals the same magic constant: n(n²+1)/2. For n=1 returns [1], for n=2 there is no magic square so returns [1 3; 4 2] (not a true magic square). Works for all n ≥ 1.
Parameters
| Name | Description |
|---|---|
| n | Size of the magic square (n ≥ 1) |
Returns
n×n magic square NDArray