ones - Create array of all ones - MATLAB (original) (raw)

Syntax

Description

[X](#mw%5Fbc9eb40d-14ab-405e-a2ef-6cb23b9b13a7) = ones returns the scalar1.

[X](#mw%5Fbc9eb40d-14ab-405e-a2ef-6cb23b9b13a7) = ones([n](#btpizpg-1-n)) returns ann-by-n matrix of ones.

example

[X](#mw%5Fbc9eb40d-14ab-405e-a2ef-6cb23b9b13a7) = ones([sz1,...,szN](#btpizpg-1-sz1szN)) returns an sz1-by-...-by-szN array of ones wheresz1,...,szN indicates the size of each dimension. For example, ones(2,3) returns a 2-by-3 array of ones.

example

[X](#mw%5Fbc9eb40d-14ab-405e-a2ef-6cb23b9b13a7) = ones([sz](#btpizpg-1-sz)) returns an array of ones where the size vector, sz, definessize(X). For example, ones([2,3]) returns a 2-by-3 array of ones.

example

[X](#mw%5Fbc9eb40d-14ab-405e-a2ef-6cb23b9b13a7) = ones(___,[typename](#btpizpg-1-classname)) also specifies the data type (class) of X for any of the previous syntaxes. For example, ones(5,"int8") returns a 5-by-5 matrix of 8-bit integers.

example

[X](#mw%5Fbc9eb40d-14ab-405e-a2ef-6cb23b9b13a7) = ones(___,like=[p](#btpizpg-1-p)) specifies that X has the same data type, sparsity, and complexity (real or complex) as the numeric variablep.

example

Examples

collapse all

Create a 4-by-4 array of ones.

X = 4×4

 1     1     1     1
 1     1     1     1
 1     1     1     1
 1     1     1     1

Create a 2-by-3-by-4 array of ones.

Define a 3-by-2 array A.

A = [1 4 ; 2 5 ; 3 6]; sz = size(A)

Create an array of ones that is the same size as A.

Create a 1-by-3 vector of ones whose elements are 16-bit unsigned integers.

X = 1×3 uint16 row vector

1 1 1

Create a scalar 1 that is not real valued, but instead is complex like an existing array.

Define a complex vector.

Create a scalar 1 that is complex like p.

Define a 2-by-3 array of 8-bit unsigned integers.

p = uint8([1 3 5; 2 4 6]);

Create an array of ones that is the same size and data type as p.

X = 2×3 uint8 matrix

1 1 1 1 1 1

Input Arguments

collapse all

Size of square matrix, specified as an integer value, defines the output as a square, n-by-n matrix of ones.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Size of each dimension, specified as two or more integer values, defines X as a sz1-by...-by-szN array.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output size, specified as a row vector of integer values. Each element of this vector indicates the size of the corresponding dimension.

Example: sz = [2 3 4] defines X as a 2-by-3-by-4 array.

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output class, specified as "double","single", "logical","int8", "uint8","int16", "uint16","int32", "uint32","int64", or "uint64".

Prototype, specified as a variable.

Data Types: double | single | logical | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
Complex Number Support: Yes

Output Arguments

collapse all

Array of ones, returned as a scalar, vector, matrix, or multidimensional array.

Extended Capabilities

expand all

Usage notes and limitations:

Usage notes and limitations:

Refer to the usage notes and limitations in the C/C++ Code Generation section. The same limitations apply to GPU code generation.

Dimensions must be real, nonnegative integers.

The ones function supports GPU array input with these usage notes and limitations:

For more information, see Run MATLAB Functions on a GPU (Parallel Computing Toolbox).

Usage notes and limitations:

For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox).

Version History

Introduced before R2006a