zeros — Create zero-filled arrays with a selected shape, class, storage, and residency.
zeros creates scalar, matrix, or N-D zero values from dimensions or a size vector. Numeric class names and like prototypes select typed storage and supported placement. For an object prototype, zeros(..., 'like', prototype) invokes its zerosLike(prototype, ...) method, or its static zeros(...) method if zerosLike is absent. Dimension arguments retain their original order and form. Method errors are returned without trying another creation method.
Syntax
A = zeros()
A = zeros(n)
A = zeros(size_vector)
A = zeros(m, n, ...)
A = zeros(prototype)
A = zeros(..., typename)
A = zeros(..., "like", prototype)Inputs
n- Type
SizeArg- Required
- Yes
- Description
- Square size.
size_vector- Type
SizeArg- Required
- Yes
- Description
- Size vector defining output dimensions.
dims- Type
SizeArg- Required
- Variadic
- Description
- Dimension sizes.
prototype- Type
LikePrototype- Required
- Yes
- Description
- Prototype value when no numeric dimension arguments are provided.
typename- Type
StringScalar- Required
- No
- Default
"double"- Description
- Class name override.
like_kw- Type
StringScalar- Required
- Yes
- Default
"like"- Description
- Like keyword.
prototype- Type
LikePrototype- Required
- Yes
- Description
- Prototype array used for class/device.
Returns
A- Type
NumericArray- Description
- Output array.
A- Type
Any- Description
- Numeric output selected by the prototype, or the object creation method's result.
Errors
- When
- The 'like' keyword is provided without a prototype argument.
- Message
zeros: expected prototype after 'like'
- When
- A class keyword and a 'like' prototype are both provided.
- Message
zeros: cannot combine 'like' with other class specifiers
- When
- A trailing option string is not a supported class keyword.
- Message
zeros: unrecognised option
- When
- The 'like' keyword is specified more than once.
- Message
zeros: multiple 'like' specifications are not supported
Behavior
zeros() returns scalar double zero. zeros(n) creates an n-by-n matrix; separate dimensions or a row size vector create the requested dense N-D shape. Size controls accept double, single, and all eight fixed-width integer classes.
A trailing class name selects double, single, logical, or any fixed-width integer output. zeros(..., "like", prototype) preserves the prototype's class, sparsity, complexity, and supported residency. Integer output uses native fixed-width storage.
RunMat mode additionally accepts a column size vector, a resident numeric size control, or a prototype without the like keyword. Use the explicit compatible forms when source must run under a MATLAB compatibility pin.
GPU execution
A resident like prototype selects its owning provider and physical element type. RunMat asks that provider for a zero-filled allocation and can fall back to a typed host upload when the provider lacks a zero-allocation hook.
The planner may also place eligible constructor results on an accelerator when doing so benefits a surrounding expression. Explicit resident prototypes retain user-selected placement and ownership.
Examples
Create scalar zero
z = zeros()Expected output:
z = 0Create a rectangular matrix
A = zeros(2, 3)Expected output:
A = [0 0 0; 0 0 0]Create a logical column
mask = zeros(4, 1, "logical")Expected output:
mask is a 4-by-1 logical array of false valuesCreate native fixed-width integer storage
counts = zeros(2, 3, "uint16")Expected output:
counts is a 2-by-3 uint16 matrixCreate an N-D array from a size vector
T = zeros([2 3 4]);
shape = size(T)Expected output:
shape = [2 3 4]Match a host prototype's class
prototype = single([1 2; 3 4]);
A = zeros(3, 2, "like", prototype)Expected output:
A is a 3-by-2 single matrixAllocate zeros with a resident prototype
prototype = gpuArray(single([1 2; 3 4]));
G = zeros(3, 2, "like", prototype);
host = gather(G)Expected output:
host is a 3-by-2 single matrixInfer shape and class from a prototype in RunMat mode
prototype = int32([1 2; 3 4]);
A = zeros(prototype)Expected output:
A is a 2-by-2 int32 zero matrixUsing zeros with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how zeros changes the result.
Run a small zeros example, explain the result, then change one input and compare the output.
FAQ
What does zeros() return?⌄
It returns scalar double zero.
What does zeros(n) return?⌄
It returns an n-by-n dense double matrix.
How do I create an N-D array?⌄
Pass each dimension separately or provide one row size vector, such as zeros([2 3 4]).
Which output classes can I request?⌄
Double, single, logical, and all eight fixed-width integer classes are supported.
How do I match another value?⌄
Use zeros(..., "like", prototype) to preserve its class, sparsity, complexity, and supported residency.
Can zeros allocate on a GPU?⌄
Yes. A resident like prototype selects the owning provider; the planner can also place eligible results automatically.
Does a provider need a native zero-allocation hook?⌄
No. RunMat can upload a correctly typed host zero value when that hook is unavailable.
Is zeros(A) portable prototype syntax?⌄
No. The implicit prototype form is a RunMat extension; use zeros(..., "like", A) for compatible source.
Is output always dense?⌄
Ordinary constructor forms are dense. A sparse like prototype preserves sparse storage.
Why preallocate with zeros?⌄
Preallocation fixes the intended class and shape before indexed assignment and can avoid repeated growth.
Related Array functions
Creation
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
Accumulation
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
Binning
Grouping
Source you can read
Every RunMat function's source is public. Read exactly how zeros is executed, line by line, in Rust.
- View the source for zeros in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
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.