In mathematical calculations in Matlab, matrices are generally used. So there are lots of easy matrices generating functions in Matlab. Sometimes users needed to obtain big matrices that include only the number of 1 in all elements inside it. This kind of matrix could be generated by a special command called ‘ones’. This is the command that the users could obtain matrices that comprises only the number ‘1’ inside them. You can follow the basic example below to understand how the ‘ones’ code works in Matlab.

**Check: Recommended Books to Learn Matlab®**

## How to Use Ones Function in Matlab?

The syntax of the ones function in Matlab is very simple. You can create vectors and matrices with all the elements 1. Check the various examples below about the ones that function in Matlab.

```
>> v = ones(2,3)
v =
1 1 1
1 1 1
```

For example, we would like to create a 2×3 matrix with all the elements being one. We can do this with the ones() function in Matlab easily. Inside the brackets of the ones() function, type the dimensions. And hit the enter key to see the result. You can see the 2×3 ones matrix in the command window.

```
v = ones(2,3)
z = 5.*v
z =
5 5 5
5 5 5
```

Also, you can use the matrix that you create with the ones() function in different mathematical operations. For example, we would like to obtain a 2×3 matrix that has all the elements of 5. So, we multiplied this one’s matrix with 5 in Matlab. If you check the result, we created the matrix which has 6 elements and all the elements are 5.

### Mathematical Operations with Ones in Matlab

```
d = 5./ones(4,5)
d =
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
```

You can also directly use the ones() function in the mathematical operations in Matlab. For instance, we divide the number 5 with the ones() function. And we used this function to create a 5×4 matrix that has all the values 1. So, all the variables of the d matrix are 5 as you see above.

### Creating Ones Square Matrix in Matlab

Square matrices are very important in mathematics and linear algebra. Matlab provides various kinds of tools to create the square matrix. You can also create ones() square matrix in Matlab. Check the coding example below to understand how to do it.

```
x = ones(2,2)
x =
2 2
2 2
```

As you see above, we tried to create a ones() matrix in Matlab which has the dimensions of 2×2. So we typed a two inside the dimensions and we obtained the 2×2 matrix which has all the elements are 2.

### Creating Arrays of Ones

```
a = ones(1,4)
a =
1 1 1 1
```

In this example, we create a vector with the array of elements 1. You can do this by using the ones() function in Matlab. You just need to specify the first element 1 to make the number of rows 1. It is very simple like above.

### Creating a Scalar with Ones Function

If you type ones directly to the command window of Matlab, it gives a scalar of 1. Check the coding example below to understand it.

```
a = ones
a =
1
```

This is the general working principle of ones functions in Matlab. We assigned the ones to function directly to the ‘a’ variable. So you can see that value of the variable ‘a’ is 1. You can multiply or divide this variable with different numbers or vectors.

### Obtaining 3D Arrays with Ones Function in Matlab

You can also create a 3D matrix in Matlab. 3D matrices are generally used in different calculations and different algebraic functions. You just need to add 1 dimension to the ones() function in Matlab. For example, the ones(1, 2, 3) function will create a 3D matrix that has 6 elements inside it.

Matlab provides various useful tools and function to create different kinds of matrices and vectors for mathematical calculations. Check the useful function of Matlab to create different matrices below.

Cumulative Multiplication of Vectors and Matrices in Matlab®

Summation of All Elements of Matrices And Vectors in Matlab®

Editing Matrices and Vectors at Variable Editor in Matlab®(Illustrated Expression)

Dot Products of Matrices and Vectors in Matlab®(Illustrated Expression)

Calculation of Absolute Values of Numbers, Vectors, and Matrices in Matlab®(Illustrated Expression)

## Conclusion

Also, you can directly do mathematical calculations with ‘ones’ code as shown by the d variable. So you can obtain various matrices by using the ‘ones()’ function as shown above in Matlab®.

Do not forget to leave your comments and questions below about the use of the ‘ones()’ function in Matlab® below.

If you want further coding examples about ‘the ‘ones()’ function in Matlab®, inform us in the comments.

This article is prepared for completely educative and informative purposes. Images used courtesy of Matlab®

Your precious feedbacks are very important to us.

## Leave a Reply