magic — Generate magic squares in MATLAB and RunMat.
magic(n) returns a double n-by-n matrix. The order is floor(real(double(n(1)))), so array and complex inputs use their first real element. Construction rules across odd/even orders follow MATLAB semantics.
Syntax
A = magic(n)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
n | NumericArray | Yes | — | Numeric input whose floored first real element selects the square order. |
Returns
| Name | Type | Description |
|---|---|---|
A | NumericArray | n-by-n magic square matrix. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | The builtin is called with a number of arguments other than one. | magic: requires exactly one input argument |
| — | The input has no numeric elements. | magic: input must contain a numeric value |
| — | The order argument is non-finite. | magic: dimension must be finite |
| — | The floored order argument is negative. | magic: dimension must be non-negative |
| — | The order is supplied as an explicit gpuArray. | magic: gpuArray input is not supported |
How magic works
magic(n)accepts documented numeric and character classes, selects the first real element, floors it, and requires the resulting order to be non-negative and representable.magic(1)returns1, whilemagic(0)returns an empty0 x 0matrix.magic(2)returns the documented degenerate matrix[1 3;4 2], which is not a true magic square.- 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 transparently gathers automatically resident inputs before evaluating magic and returns a host tensor. Explicit gpuArray input is rejected because the documented API does not support it.
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 2A 4x4 magic square
M = magic(4)Expected output:
M =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1Use 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
Is magic(2) a true magic square?⌄
No. It returns [1 3;4 2] for compatibility, even though no true 2x2 magic square exists.
What happens when n is an array?⌄
The first element controls the order after taking its real part and flooring it. 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.
Related Array functions
Creation
colon · createArray · empty · eye · false · full · inf · linspace · logspace · meshgrid · nan · nchoosek · ndgrid · nonzeros · ones · peaks · perms · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · zeros
Grouping
accumarray · combinations · discretize · findgroups · groupcounts · grp2idx · splitapply
Sorting Sets
argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how magic is executed, line by line, in Rust.
- View the source for magic in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.