Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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.

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.

Value

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

Note

A magic square, scaled by its magic sum, is doubly stochastic.

Author(s)

P. Roebuck roebuck@mdanderson.org for the first R version in the package ‘matlab’. The version here is more R-like.

Examples

magic(3)

pracma

Practical Numerical Math Functions

v2.3.3
GPL (>= 3)
Authors
Hans W. Borchers [aut, cre]
Initial release
2021-01-22

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.