Matlab

# ones function in MatLab

The following article explains how to use the ones() function in MATLAB® to create arrays with elements of value 1. You will also learn about the main properties of the function and various ways to use the options of this function to handle and create 2D and 3D arrays with different data types. This topic includes working examples and images to help you understand how the ones() function works in MATLAB®.

## ones function Syntax

a = ones
a = ones(n)
a = ones(z1……zn)
a = ones (….., typename)
a = ones (….., ‘like’, p)

## Description and examples

The ones() function creates 2D and 3D arrays whose elements all contain the value 1. You can also create scalars with this value. Although MATLAB has the ones() and zeros() functions with the same properties, the types of data it returns in the arrays are not logical, but can be double, single, int8, int16, int32, int64, etc. uint8, uint16, uint32, or uint64. The type of data the array should contain can be selected when the function is called.

Next, we will see how each of the input arguments of ones() works.

n: This input specifies the size of the square matrix created by ones().

z1…. zn: This input specifies the size of the square matrix created by ones().

typename: This input sets the data type that the output array should have.

like: When the ones() function is called with this flag, the data type in the output array will be equal to the data type of “p.”

p: Prototype. When the ones() function is called with the ‘like’ flag, the data type in the output array will be equal to the data type of “p”

## How to create a scalar with value 1 from the MATLAB ones() function

The following example shows the simplest way to use the ones() function by creating a scalar with the value 1. In this case, ones() create a scalar “a” and store the value 1 in it.

a = ones

As a result, the ones() function will return the next scalar.

a = 1

Note how in this case, the function is called without input arguments and in its simplest form.

## How to create a square matrix with the MATLAB ones() function

Now let us see how we use the function ones() to create a square matrix of n rows x n columns whose elements contain the value 1. In this case, we create a 5×5 matrix by specifying as input argument in “n” the number of rows/columns of the matrix we want to get in “a.”

a = ones(5)
a =

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

As a result, the ones() function will return the following 5×5 element square matrix.

## How to create an array of 1 with different dimensions using MATLAB’s ones() function

In the previous example, we saw how to create a square matrix of ones. Now we will see how to create an array of ones with a different number of columns and rows. To do this, we create an array with 4×5 elements by passing the ones() function the number of columns in “z1” and the number of rows in “z2” as input arguments, as shown below.

a = ones(4, 5)

As a result, ones() will return in “a” an array of 1 of 4×5 elements.

a =

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

## How to create a matrix of 1 with the MATLAB function ones() using a vector to indicate its size

It is also possible to create a matrix of 1 from the vector of size “z,” as shown below. To do this, we call the function ones() and pass as input argument a vector containing the number of columns and rows of the matrix we want to create. In this case, it is an array with 3×4 elements.

a =ones([3, 4])

As a result, ones() will return the following array in “a.”

a =

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

## How to create a 3D array of elements with value 1 using MATLAB’s ones() function

With the function ones(), it is also possible to create 3D arrays. In the following example, we see how to create a matrix of 1 in three dimensions of 2x4x3 using this function.

a = ones(2, 4, 3)

As a result, ones() returns a 2x4x3 3D array with all its elements set to 1.

a(:,:,1) =

1     1     1     1
1     1     1     1

a(:,:,2) =

1     1     1     1
1     1     1     1

a(:,:,3) =

1     1     1     1
1     1     1     1

## How to select the data type of the matrix created with the “typename” option of the ones() function in MATLAB

Next, we will look at how to select the data type of the elements that make up the matrix created with ones(). In this example, we will create a 3×3 element matrix of unsigned 32-bit integers. To do this, we call the function ones() as follows:

a = ones(3, 3, ‘uint32’)

As a result, ones() returns in “a” an array of 3×3 32-bit unsigned integers containing a value of 1.

a =

3×3 uint32 matrix

1   1   1
1   1   1
1   1   1

## How to create an array of 1 with the same data type as a specified variable using the “like” option of the ones() function in MATLAB

Con la función ones() es posible crear una matriz de unos que tenga el mismo tipo de datos que una variable específica. En este ejemplo, creamos un escalar “p” de tipo uint64 y lo usamos para crear una matriz “a” de 3×3 con el mismo tipo de datos que “p”.

u = uint64(12);
a =ones (3, 3, 'like',p)

In this case, ones will return a 3×3 array with elements of the same type as the variable “u” created above.

a =
3×3 uint64 matrix

1   1   1
1   1   1
1   1   1

## Conclusion

In this article, we explained how to use the MATLAB function ones() to create 2D and 3D arrays of ones. The various options that this function offers in handling input and output data were also explained in detail. This topic included some practical examples with different arrays and data types. The argument types supported by each and the accepted data types were also explained in detail. We hope that you found this MATLAB article helpful. See other Linux Hint articles for more tips and information.