RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.1, Nov 17, 2026

RunMat® is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
See all docs
Builtin Reference
    • colon
    • createArray
    • empty
    • eye
    • false
    • full
    • inf
    • int16.empty
    • int32.empty
    • int64.empty
    • int8.empty
    • linspace
    • logspace
    • magic
    • meshgrid
    • nan
    • ndgrid
    • nonzeros
    • ones
    • peaks
    • rand
    • randi
    • randn
    • randperm
    • range
    • sparse
    • spdiags
    • speye
    • spones
    • sprand
    • true
    • uint16.empty
    • uint32.empty
    • uint64.empty
    • uint8.empty
    • zeros

magic — Generate magic squares in MATLAB and RunMat.

magic(n) returns a host double n-by-n matrix. The first real element selects the order; floating-point values are floored and integer values are decoded exactly. Odd, doubly-even, and singly-even orders use separate constructions.

Syntax

A = magic(n)

Inputs

n
Type
Any
Required
Yes
Description
Numeric, logical, or character input whose first real element selects the square order; floating values are floored.
NameTypeRequiredDefaultDescription
nAnyYes—Numeric, logical, or character input whose first real element selects the square order; floating values are floored.

Returns

A
Type
NumericArray
Description
n-by-n magic square matrix.
NameTypeDescription
ANumericArrayn-by-n magic square matrix.

Errors

RunMat:magic:TooManyOutputs
When
More than one output is requested.
Message
magic: too many output arguments
—
When
The builtin is called with a number of arguments other than one.
Message
magic: requires exactly one input argument
—
When
The input has no numeric elements.
Message
magic: input must contain a numeric value
IdentifierWhenMessage
RunMat:magic:TooManyOutputsMore than one output is requested.magic: too many output arguments
—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

Behavior

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) returns 1, while magic(0) returns an empty 0 x 0 matrix.

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.

GPU residency and provider behavior

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

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.

Computed on the host CPU. Automatically resident inputs gather transparently; explicit gpuArray input is unsupported.

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

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 · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · meshgrid · nan · ndgrid · nonzeros · ones · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros

Accumulation

accumarray

Sorting Sets

argsort · intersect · ismember · ismembertol · issorted · issortedrows · setdiff · setxor · sort · sortrows · union · unique

Shape

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

Combinatorics

combinations · nchoosek · perms

Binning

discretize

Indexing

find · ind2sub · sub2ind

Grouping

findgroups · groupcounts · grp2idx · splitapply

Introspection

height · iscolumn · isempty · ismatrix · isrow · isscalar · isvector · length · ndims · numel · size · width

About RunMat

RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/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.

Download RunMatOpen Sandbox
On this page
  • Syntax
  • Inputs
  • Returns
  • Errors
  • Behavior
  • GPU residency and provider behavior
  • Examples
  • A 3x3 magic square
  • A 4x4 magic square
  • Use magic squares as structured input
  • Using magic with coding agents
  • FAQ
  • Related Array functions
  • Creation
  • Accumulation
  • Sorting Sets
  • Shape
  • Combinatorics
  • Binning
  • Indexing
  • Grouping
  • Introspection
  • About RunMat