ones — Create arrays of ones in MATLAB and RunMat.
ones creates arrays filled with ones. Scalar, vector, matrix, and N-D forms (including 'like' and 'logical' options) follow MATLAB semantics.
Syntax
A = ones()
A = ones(n)
A = ones(size_vector)
A = ones(m, n, ...)
A = ones(prototype)
A = ones(..., typename)
A = ones(..., "like", prototype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
n | SizeArg | Yes | — | Square size. |
size_vector | SizeArg | Yes | — | 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 name override (double|single|logical). |
like_kw | StringScalar | Yes | "like" | Like keyword. |
prototype | LikePrototype | Yes | — | Prototype array used for class/device. |
Returns
| Name | Type | Description |
|---|---|---|
A | NumericArray | Output array. |
Errors
| Identifier | When | Message |
|---|---|---|
| — | The 'like' keyword is provided without a prototype argument. | ones: expected prototype after 'like' |
| — | A class keyword and a 'like' prototype are both provided. | ones: cannot combine 'like' with other class specifiers |
| — | A trailing option string is not a supported class keyword. | ones: unrecognised option |
| — | The 'like' keyword is specified more than once. | ones: multiple 'like' specifications are not supported |
How ones works
ones()returns the scalar1.ones(n)returns ann × ndouble array.ones(m, n, ...)returns a dense double array with the requested dimensions.ones(sz)accepts a size vector (row or column) and returns an array withprod(sz)elements arranged using MATLAB column-major ordering.ones(A)returns an array of ones with the same size (and device residency) asA.ones(___, 'logical')returns a logical array instead of double precision (all elements set totrue).ones(___, 'like', prototype)matches the device residency and numeric/logical flavour ofprototype.
Does RunMat run ones on the GPU?
When the prototype or 'like' argument is a GPU tensor, RunMat asks the active acceleration provider to allocate a one-filled buffer. Acceleration providers that do not yet support the dedicated hooks fall back to zero-allocation plus scalar fill or, as a last resort, upload a host tensor. This guarantees correct behaviour.
GPU memory and residency
You usually do NOT need to call gpuArray yourself in RunMat (unlike MATLAB).
In RunMat, the fusion planner keeps residency on GPU in branches of fused expressions. As such, in the above example, the result of the ones call will already be on the GPU when the fusion planner has detected a net benefit to operating the fused expression it is part of on the GPU.
To preserve backwards compatibility with MathWorks MATLAB, and for when you want to explicitly bootstrap GPU residency, you can call gpuArray explicitly to move data to the GPU if you want to be explicit about the residency.
Since MathWorks MATLAB does not have a fusion planner, and they kept their parallel execution toolbox separate from the core language, as their toolbox is a separate commercial product, MathWorks MATLAB users need to call gpuArray to move data to the GPU manually whereas RunMat users can rely on the fusion planner to keep data on the GPU automatically.
Examples
Creating a 3x3 matrix of ones
B = ones(3)Expected output:
B = [1 1 1; 1 1 1; 1 1 1]Creating a 4x1 logical matrix of ones
mask = ones(4, 1, 'logical')Expected output:
mask = [1 1 1 1]Creating a 64x64 matrix of ones on a GPU
H = ones(64, 64)Expected output:
H = [1 1 1 ... 1; 1 1 1 ... 1; ...; 1 1 1 ... 1]Using ones with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how ones changes the result.
Run a small ones example, explain the result, then change one input and compare the output.
FAQ
When should I use the ones function?⌄
Use ones whenever you need to create arrays pre-filled with the value 1, such as for initializing weights, creating masks, or testing other algorithms. Preallocating with ones ensures correct type and shape throughout your code and helps prevent bugs caused by uninitialized arrays.
Does ones produce double arrays by default?⌄
Yes, by default, ones creates dense double-precision arrays unless you explicitly specify a type such as 'logical' or use the 'like' argument to match a prototype array.
What does ones(n) return?⌄
ones(n) returns an n × n dense double-precision matrix filled with ones. For example, ones(3) yields a 3-by-3 matrix of all ones.
How do I create a logical array of ones?⌄
Pass 'logical' as the last argument:
mask = ones(5, 1, 'logical');This produces a 5x1 logical array, i.e., all elements have value true (1 in binary).
How do I match the type and device residency of an existing array?⌄
Use the 'like', prototype syntax:
A = gpuArray(rand(2,2));
B = ones(2, 2, 'like', A);B will be a GPU array with the same type and shape as A.
Can I create N-dimensional arrays with ones?⌄
Yes! Pass more than two dimension arguments (or a size vector):
T = ones(2, 3, 4);This creates a 2×3×4 tensor of ones.
How does ones(A) behave?⌄
If you call ones(A), where A is an array, the result is a new array of ones with the same shape as A.
Is the output always dense?⌄
Yes. ones always produces a dense array. For sparse matrices of ones, use sparse with appropriate arguments.
What if I call ones with no arguments?⌄
ones() returns the scalar value 1.
Can I use ones to preallocate arrays for later assignment?⌄
Absolutely. Preallocating with ones (or zeros) and then filling in values is a recommended practice for efficiency and code clarity when the final values are known to overwrite the initial ones.
Related Array functions
Creation
colon · eye · false · fill · linspace · logspace · magic · meshgrid · peaks · rand · randi · randn · randperm · range · true · zeros
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how ones is executed, line by line, in Rust.
- View the source for ones 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.