ones

Array of ones.

Category: Language Fundamentals > Matrices and Arrays

Syntax

A = ones(n)

n-by-n matrix of ones.

A = ones(m, n, ...)

Array of the given size.

A = ones(sz)

Size given as a vector.

A = ones(___, classname)

Result of the given class, such as 'int8' or 'single'.

A = ones(___, 'like', p)

Same class as the prototype p.

Description

ones is the standard way to start an array of a constant: 5*ones(3) is a 3-by-3 matrix of fives, and ones(n, 1) is a column of ones, useful as the constant column of a design matrix. For the logical equivalent see true.

Input arguments

n, m, ...
Dimension sizes. A zero gives an empty array.
classname
Class of the result; default 'double'.

Output arguments

A
Array filled with 1.

Example

ones(2, 3)
5*ones(2)
ones(2, 'int8')
class(ones(1, 'uint8'))
Output
ans = 2×3

     1     1     1
     1     1     1
ans = 2×2

     5     5
     5     5
ans = 2×2 int8 matrix

   1   1
   1   1
ans = 'uint8'

See also

zeros eye true repmat rand