fill — Create arrays filled with a constant value in MATLAB and RunMat.
fill(value, size, ...) creates dense arrays whose elements are all set to value. Scalar, size-vector, and variadic-dimension forms match MATLAB semantics. 'logical' and 'like' options control output type and residency.
Syntax
A = fill(value)
A = fill(value, n)
A = fill(value, size_vector)
A = fill(value, m, n, ...)
A = fill(value, prototype)
A = fill(value, ..., typename)
A = fill(value, ..., "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
value | Any | Yes | — | Scalar fill value. |
n | SizeArg | No | — | Square size. |
size_vector | SizeArg | No | — | Size vector defining output dimensions. |
dims | SizeArg | Variadic | — | Dimension sizes. |
prototype | LikePrototype | Yes | — | Prototype value when no numeric dimension arguments are provided. |
typename | StringScalar | No | "double" | Class override ('double'|'logical'|'complex'). |
like_kw | StringScalar | Yes | "like" | Like keyword. |
prototype | LikePrototype | Yes | — | Prototype array used for class/device. |
Returns
| Name | Type | Description |
|---|---|---|
A | Any | Constant-valued output array. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | The fill value is not scalar. | fill: fill value must be a scalar |
| — | The fill value type is unsupported. | fill: fill value must be numeric, logical, or complex scalar |
| — | The 'like' keyword is provided without a prototype argument. | fill: expected prototype after 'like' |
| — | The 'like' keyword is provided multiple times. | fill: multiple 'like' specifications are not supported |
| — | A class keyword and a 'like' prototype are both provided. | fill: cannot combine 'like' with class specifiers |
| — | An unsupported output class is requested. | fill: single precision output is not implemented yet |
| — | A trailing option string is not recognized. | fill: unrecognised option |
| — | Dimension arguments fail numeric/shape parsing. | fill: dimension arguments must be numeric and nonnegative |
How fill works
fill(v)returns the scalarv.fill(v, n)returns ann × nmatrix with every element equal tov.fill(v, m, n, ...)supports arbitrary N-D dimensions.fill(v, sz)accepts a size vector (row or column).fill(v, A)uses the shape (and default type) ofA.fill(v, ___, 'logical')produces a logical array where each element isv ~= 0.fill(v, ___, 'like', prototype)matches the type, device residency, and shape ofprototype.- When a GPU prototype or
'like'argument is provided, RunMat keeps the result on the device. When provider hooks are incomplete, it falls back to uploading a host tensor to guarantee correctness.
Does RunMat run fill on the GPU?
RunMat asks the active acceleration provider to materialise the constant directly via the dedicated fill hook. Providers that do not supply this capability fall back to a zero buffer plus scalar add or, as a last resort, upload the fully materialised host tensor. This guarantees MATLAB-compatible results while still benefiting from GPU residency whenever possible.
GPU memory and residency
A = rand(512, 512);
J = fill(0.5, size(A), 'like', gpuArray(A));Expected output:
isa(J, 'gpuArray') % trueExamples
Creating a 3x3 matrix of 2.5s
A = fill(2.5, 3)Expected output:
A = [2.5 2.5 2.5; 2.5 2.5 2.5; 2.5 2.5 2.5]Filling a rectangular matrix with -4
B = fill(-4, 2, 5)Expected output:
B = [-4 -4 -4 -4 -4; -4 -4 -4 -4 -4]Filling using a size vector
sz = [2 3 4];
C = fill(10, sz)Expected output:
size(C) % 2-by-3-by-4Creating a logical mask with fill
mask = fill(3, 4, 1, 'logical')Expected output:
mask = [1; 1; 1; 1]Matching an existing array with 'like'
prototype = rand(2, 3, 'like', gpuArray(1));
D = fill(pi, 2, 3, 'like', prototype);
gather(D)Expected output:
3.1416 3.1416 3.1416 3.1416 3.1416 3.1416Filling a complex array
E = fill(1 + 2i, 2, 2, 'complex')Expected output:
E = [1+2i 1+2i; 1+2i 1+2i]Using a prototype to infer shape automatically
F = rand(4, 2);
G = fill(7, F)Expected output:
isequal(size(G), [4 2]) % trueFilling an empty matrix
H = fill(0, 0, 5)Expected output:
size(H) % 0-by-5Keeping GPU residency without explicit gpuArray
A = rand(512, 512);
J = fill(0.5, size(A), 'like', gpuArray(A))Expected output:
isa(J, 'gpuArray') % trueFilling with boolean semantics
K = fill(false, [2 2], 'logical')Expected output:
K = [0 0; 0 0]Using fill with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fill changes the result.
Run a small fill example, explain the result, then change one input and compare the output.
FAQ
Which argument order does fill support?⌄
The first argument is always the scalar value. Remaining arguments specify dimensions, size vectors, or options ('logical', 'like', 'double', 'complex').
Does fill accept non-scalar values?⌄
No. The fill value must be numeric, logical, or complex scalar. Use repmat when you need to tile an array.
How does fill behave with complex values?⌄
If you request complex output (explicitly or via 'like'), the real and imaginary parts are copied verbatim. Otherwise the imaginary component must be zero.
How do logical arrays handle nonzero values?⌄
Logical outputs treat any nonzero real or complex magnitude as true, matching MATLAB semantics.
Can I match GPU residency automatically?⌄
Yes. Pass a GPU prototype via 'like' and fill will allocate on the device. It falls back to host allocation and upload when the provider cannot construct the constant directly.
What happens when I omit dimensions?⌄
If you only pass the value, fill returns a scalar. When you pass a prototype (e.g., fill(v, A)), the prototype shape is reused. Otherwise RunMat follows MATLAB's default of 1 × 1.
Are string or char outputs supported?⌄
Not yet. fill currently targets numeric, logical, and complex arrays. Use string-manipulation functions for text.
How do I create empty outputs?⌄
Include a zero dimension (fill(value, 0, n) or fill(value, [0 n])). RunMat returns an empty array of the requested size and type.
Does fill honour 'single' outputs?⌄
Single precision outputs are not implemented yet. The function returns an error mirroring other array creation builtins.
Is the GPU path deterministic?⌄
Yes. Providers either execute the constant fill entirely on the GPU or RunMat uploads a deterministic host tensor, ensuring identical results.
Related Array functions
Creation
colon · eye · false · linspace · logspace · magic · meshgrid · ones · peaks · rand · randi · randn · randperm · range · true · zeros
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how fill is executed, line by line, in Rust.
- View the source for fill 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.