Skip to content

magic

Magic square — square matrix where every row, column, and diagonal sums to the same value.

M = magic(n)

Returns an n×n matrix of the integers 1:n^2 arranged so that every row, every column, and both main diagonals share the same sum. n must be a positive integer. Often used as test data because the structure is unambiguous.

magic(3) % [8 1 6; 3 5 7; 4 9 2]
sum(magic(5)) % uniform column sums
sum(magic(5), 2) % uniform row sums
  • pascal — Pascal matrix — symmetric matrix of binomial coefficients.
  • eye — Identity matrix.
  • zeros — Array of all zeros.