# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# ones

Create array of all ones

## Syntax

• ``X = ones``
• ``X = ones(n)``
example
• ``X = ones(sz1,...,szN)``
example
• ``X = ones(sz)``
example
• ``X = ones(classname)``
• ``X = ones(n,classname)``
• ``X = ones(sz1,...,szN,classname)``
example
• ``X = ones(sz,classname)``
• ``X = ones('like',p)``
example
• ``X = ones(n,'like',p)``
• ``X = ones(sz1,...,szN,'like',p)``
• ``X = ones(sz,'like',p)``
example

## Description

``X = ones` returns the scalar `1`.`

example

````X = ones(n)` returns an `n`-by-`n` matrix of ones.```

example

````X = ones(sz1,...,szN)` returns an `sz1`-by-...-by-`szN` array of ones where `sz1,...,szN` indicates the size of each dimension. For example, `ones(2,3)` returns a 2-by-3 array of ones.```

example

````X = ones(sz)` returns an array of ones where the size vector, `sz`, defines `size(X)`. For example, `ones([2,3])` returns a 2-by-3 array of ones.```
````X = ones(classname)` returns a scalar `1` where `classname` specifies the data type. For example, `ones('int8')` returns a scalar, 8-bit integer `1`.```
````X = ones(n,classname)` returns an `n`-by-`n` array of ones of data type `classname`.```

example

````X = ones(sz1,...,szN,classname)` returns an `sz1`-by-...-by-`szN` array of ones of data type `classname`.```
````X = ones(sz,classname)` returns an array of ones where the size vector, `sz`, defines `size(X)` and `classname` defines `class(X)`.```

example

````X = ones('like',p)`returns a scalar `1` with the same data type, sparsity, and complexity (real or complex) as the numeric variable, `p`.```
````X = ones(n,'like',p)` returns an `n`-by-`n` array of ones like `p`.```
````X = ones(sz1,...,szN,'like',p)` returns an `sz1`-by-...-by-`szN` array of ones like `p`.```

example

````X = ones(sz,'like',p)` returns an array of ones like `p` where the size vector, `sz`, defines `size(X)`.```

## Examples

collapse all

Create a 4-by-4 array of ones.

```X = ones(4) ```
```X = 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.

```X = ones(2,3,4); size(X) ```
```ans = 2 3 4 ```

Define a 3-by-2 array `A`.

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

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

```X = ones(sz) ```
```X = 1 1 1 1 1 1 ```

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

```X = ones(1,3,'uint16'), class(X) ```
```X = 1×3 uint16 row vector 1 1 1 ans = uint16 ```

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

Define a complex vector.

```p = [1+2i 3i]; ```

Create a scalar `1` that is complex like `p`.

```X = ones('like',p) ```
```X = 1.0000 + 0.0000i ```

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 = ones(size(p),'like',p), class(X) ```
```X = 2×3 uint8 matrix 1 1 1 1 1 1 ans = uint8 ```

## 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.

• If `n` is `0`, then `X` is an empty matrix.

• If `n` is negative, then it is treated as `0`.

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.

• If the size of any dimension is `0`, then `X` is an empty array.

• If the size of any dimension is negative, then it is treated as `0`.

• If any trailing dimensions greater than `2` have a size of `1`, then the output, `X`, does not include those dimensions.

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.

• If the size of any dimension is `0`, then `X` is an empty array.

• If the size of any dimension is negative, then it is treated as `0`.

• If any trailing dimensions greater than `2` have a size of `1`, then the output, `X`, does not include those dimensions.

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'`.

Data Types: `char`

Prototype, specified as a variable.

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