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(..., typename)
A = ones(..., "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.
typename- Type
StringScalar- Required
- No
- Default
"double"- Description
- Class name override (double|single|logical|int8|int16|int32|int64|uint8|uint16|uint32|uint64).
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.
Errors
- When
- The 'like' keyword is provided without a prototype argument.
- Message
ones: expected prototype after 'like'
- When
- A class keyword and a 'like' prototype are both provided.
- Message
ones: cannot combine 'like' with other class specifiers
- When
- A trailing option string is not a supported class keyword.
- Message
ones: unrecognised option
- When
- The 'like' keyword is specified more than once.
- Message
ones: multiple 'like' specifications are not supported
Behavior
ones() returns the scalar 1.
ones(n) returns an n × n double 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 with prod(sz) elements arranged using MATLAB column-major ordering.
A numeric size vector specifies dimensions; an implicit array prototype is rejected. Use the like option to select a prototype's class or residency.
ones(___, 'logical') returns a logical array instead of double precision (all elements set to true).
ones(___, 'like', prototype) matches the device residency and numeric/logical flavour of prototype.
Execution and residency
Ordinary ones constructs a host result. An explicit gpuArray class or resident like prototype requests an owning-provider result; automatic fusion may keep suitable values resident when selected.
Uses provider one-allocation hooks when available; otherwise fills via scalar add or uploads from the host.
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
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 do I match an array prototype?⌄
Use ones(size(A), 'like', A) for A's dimensions and class. A bare array argument is treated as a size control, not as an implicit prototype.
Is the output always dense?⌄
Ordinary constructors produce dense arrays. A sparse like prototype selects sparse output when its requested shape is supported.
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 · createArray · empty · eye · false · full · inf · int16.empty · int32.empty · int64.empty · int8.empty · linspace · logspace · magic · meshgrid · nan · ndgrid · nonzeros · peaks · rand · randi · randn · randperm · range · sparse · spdiags · speye · spones · sprand · true · uint16.empty · uint32.empty · uint64.empty · uint8.empty · zeros
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 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 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.