RunMat
GitHub

magic — Generate magic squares in MATLAB and RunMat.

magic(n) returns an n-by-n matrix containing 1:n^2 with equal row, column, and main-diagonal sums. Construction rules across odd/even orders follow MATLAB semantics.

Syntax

A = magic(n)

Inputs

NameTypeRequiredDefaultDescription
nIntegerScalarYesOrder of the magic square.

Returns

NameTypeDescription
ANumericArrayn-by-n magic square matrix.

Errors

IdentifierWhenMessage
The builtin is called with a number of arguments other than one.magic: requires exactly one input argument
The input is not a numeric scalar.magic: input must be a numeric scalar
The order argument is non-finite.magic: dimension must be finite

How magic works

  • magic(n) requires a single non-negative integer scalar n.
  • magic(1) returns 1, while magic(0) returns an empty 0 x 0 matrix.
  • magic(2) is undefined and raises an error (no 2x2 magic square exists).
  • Odd, doubly-even (n % 4 == 0), and singly-even (n % 4 == 2) orders each use MATLAB's standard construction so results match MATLAB outputs.

Does RunMat run magic on the GPU?

RunMat gathers any GPU-resident inputs before evaluating magic and returns a host tensor. No acceleration provider hooks are defined for this builtin yet.

GPU memory and residency

magic always runs on the host CPU today. If you need the result on the GPU, wrap the call in gpuArray(...).

Examples

A 3x3 magic square

M = magic(3)

Expected output:

M =
     8     1     6
     3     5     7
     4     9     2

A 4x4 magic square

M = magic(4)

Expected output:

M =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

Use magic squares as structured input

A = magic(5);
result = A * A'

Using magic with coding agents

Open a RunMat example with live inputs, then ask the agent to explain how magic changes the result.

Run a small magic example, explain the result, then change one input and compare the output.

FAQ

Why does magic(2) fail?

No 2x2 magic square exists, so MATLAB (and RunMat) report an error for order 2.

Does magic accept a size vector?

No. magic accepts a single scalar order. Use ones, zeros, or eye when you need a size vector form.

How do I move the result to the GPU?

Call gpuArray(magic(n)) if you need the output on the GPU. RunMat computes magic on the host today.

Creation

colon · eye · false · fill · linspace · logspace · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros

Sorting Sets

argsort · intersect · ismember · issorted · setdiff · sort · sortrows · union · unique

Shape

cat · circshift · diag · flip · fliplr · flipud · horzcat · ipermute · kron · permute · repelem · repmat · reshape · rot90 · squeeze · tril · triu · vertcat

Indexing

find · ind2sub · sub2ind

Introspection

isempty · ismatrix · isscalar · isvector · length · ndims · numel · size

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how magic is executed, line by line, in Rust.

About RunMat

RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.

  • RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
  • Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.