Main Content

# ones

Create array of all ones for GPU or distributed arrays

## Syntax

``N = ones(n,arraytype)``
``N = ones(size1,...,sizeN,arraytype)``
``N = ones(size,arraytype)``
``N = ones(n,datatype,arraytype)``
``N = ones(size1,...,sizeN,datatype,arraytype)``
``N = ones(size,datatype,arraytype)``
``N = ones(n,'like',P)``
``N = ones(size1,...,sizeN,'like',P)``
``N = ones(size,'like',P)``
``N = ones(n,datatype,'like',P)``
``N = ones(size1,...,sizeN,datatype,'like',P)``
``N = ones(size,datatype,'like',P)``
``C = ones(n,codist)``
``C = ones(size1,...,sizeN,codist)``
``C = ones(size,codist)``
``C = ones(n,datatype,codist)``
``C = ones(size1,...,sizeN,datatype,codist)``
``C = ones(size,datatype,codist)``
``C = ones(___,codist,'noCommunication')``
``C = ones(___,codist,'like',P)``

## Description

example

````N = ones(n,arraytype)` returns an n-by-n matrix with underlying class of double, ones in all elements, and the type specified by `arraytype`.```
````N = ones(size1,...,sizeN,arraytype)` returns a size1-by-...-by-sizeN matrix with underlying class of double, ones in all elements, and the type specified by `arraytype`.```
````N = ones(size,arraytype)` returns an array whose size is defined by the size vector size with underlying class of double, ones in all elements, and the type specified by `arraytype`.```

example

````N = ones(n,datatype,arraytype)` returns an n-by-n matrix with underlying class of `datatype`, ones in all elements, and the type specified by `arraytype`.```
````N = ones(size1,...,sizeN,datatype,arraytype)` returns a size1-by-...-by-sizeN matrix with underlying class of `datatype`, ones in all elements, and the type specified by `arraytype`.```
````N = ones(size,datatype,arraytype)` returns an array whose size is defined by the size vector size with underlying class of `datatype`, ones in all elements, and the type specified by `arraytype`.```
````N = ones(n,'like',P)` returns an n-by-n array of ones with the same underlying class and type as the prototype array, `P`.```
````N = ones(size1,...,sizeN,'like',P)` returns a size1-by-...-by-sizeN array of ones with the same underlying class and type as the prototype array, `P`.```
````N = ones(size,'like',P)` returns an array of ones whose size is defined by the size vector size with the same underlying class and type as the prototype array, `P`.```
````N = ones(n,datatype,'like',P)` returns an n-by-n array with underlying class of `datatype`, ones in all elements, and the same type as the prototype array, `P`.```
````N = ones(size1,...,sizeN,datatype,'like',P)` returns a size1-by-...-by-sizeN array with underlying class of `datatype`, ones in all elements, and the same type as the prototype array, `P`.```
````N = ones(size,datatype,'like',P)` returns an array whose size is defined by the size vector size with underlying class of `datatype`, ones in all elements, and the same type as the prototype array, `P`.```
````C = ones(n,codist)` returns an n-by-n codistributed array with underlying class of double and ones in all elements. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```
````C = ones(size1,...,sizeN,codist)` returns a size1-by-...-by-sizeN codistributed array with underlying class of double and ones in all elements. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```
````C = ones(size,codist)` returns a codistributed array whose size is defined by the size vector size with underlying class of double and ones in all elements. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```
````C = ones(n,datatype,codist)` returns an n-by-n codistributed array with underlying class of `datatype` and ones in all elements. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```

example

````C = ones(size1,...,sizeN,datatype,codist)` returns a size1-by-...-by-sizeN codistributed array with underlying class of `datatype` and ones in all elements. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```
````C = ones(size,datatype,codist)` returns a codistributed array whose size is defined by the size vector size with underlying class of `datatype` and ones in all elements. The codistributor object `codist` specifies the distribution scheme for creating the codistributed array. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`.```
````C = ones(___,codist,'noCommunication')` specifies that no interworker communication is to be performed when constructing a codistributed array, skipping some error checking steps. ```
````C = ones(___,codist,'like',P)` creates a codistributed array of ones with the specified size, underlying class, and distribution scheme. If either the class or codistributor argument is omitted, the characteristic is acquired from the codistributed array `P`.```

## Examples

collapse all

Create a 1000-by-1000 distributed array of ones with underlying class double.

```D = ones(1000,'distributed'); ```

Create a 1000-by-1000 codistributed double matrix of ones, distributed by its second dimension (columns).

```spmd(4) C = ones(1000,'codistributed'); end```

With four workers, each worker contains a 1000-by-250 local piece of `C`.

Create a 1000-by-1000 codistributed `uint16` matrix of ones, distributed by its columns.

```spmd(4) codist = codistributor('1d',2,100*[1:numlabs]); C = ones(1000,1000,'uint16',codist); end```

Each worker contains a 100-by-`labindex` local piece of `C`.

Create a 1000-by-1000 gpuArray of ones, with underlying class `uint32`:

```G = ones(1000,'uint32','gpuArray'); ```

## Input Arguments

collapse all

Size of the generated array, specified as an integer value.

Size of each dimension of the generated array, specified as separate arguments of two or more integer values.

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

Type of the generated array, specified as either `'distributed'`, `'codistributed'`, or `'gpuArray'`.

Underlying class of the array, that is the data type of its elements, specified as one of these options:

• `'double'`

• `'single'`

• `'int8'`

• `'uint8'`

• `'int16'`

• `'uint16'`

• `'int32'`

• `'uint32'`

• `'int64'`

• `'uint64'`

Distribution scheme for creating the codistributed array, specified as a codistributor object. For information on constructing codistributor objects, see the reference pages for `codistributor1d` and `codistributor2dbc`. To use the default distribution scheme, you can specify a codistributor constructor without arguments.

Prototype of array to create, specified as an array.

## Output Arguments

collapse all

Array of ones, returned as either a distributed array, a codistributed array, or a gpuArray.

Array of ones, returned as a codistributed array.

## See Also

Introduced in R2006b

## Support Get trial now