Learn R Programming

pracma (version 1.7.3)

magic: Magic Square

Description

Create a magic square.

Usage

magic(n)

Arguments

n
numeric scalar specifying dimensions for the result; n must be a scalar greater than or equal to 3.

Value

  • Returns an n-by-n matrix constructed from the integers 1 through N^2 with equal row and column sums.

Details

A magic square is a square matrix where all row and column sums and also the diagonal sums all have the same value.

This value or the characteristic sum for a magic square of order $n$ is $sum(1:n^2)/n$.

Examples

Run this code
magic(3)

Run the code above in your browser using DataLab