Matlab one’s function is used to get a scalar or a vector with all ‘ones’ (1s) as its elements. Loops like ‘while’ & ‘for’ increase the size of our data structure incrementally every time a loop is executed. This process adversely affects the performance of the program. Resizing arrays repeatedly requires extra time seeking bigger contiguous memory blocks & then placing the arrays into these blocks. Functions like Matlab ones are used to improve code performance by pre-allocating the space required for the input array.
Syntax of Function ‘ones’ in Matlab:
A = ones
A = ones (n)
A = ones (size 1, size 2, size 3, ….)
Description:
- A = ones will return scalar 1 as output.
- A = ones (n) will return a square matrix of order n X n.
- A = ones (size 1, size 2, size 3, ….) will return a matrix of required dimensions.
Examples of Matlab ones
Given below are the examples of Matlab ones:
Example 1:
In the first example, we will use ‘one’s function’ to create a square matrix of order 3 with all 1s as its elements.
Below are the steps that we will follow for this example:
- First, pass the required order as an argument to the one’s function.
- Display the output.
Code:
A = ones(3)[Using ‘ones function’ with ‘3’ as the argument. Please note that we have passed ‘3’ as an argument because we need the matrix with 1s to be of order 3 x 3 ]
A[Displaying the output]
This is how our input and output will look like in the Matlab command window:
Input:
A = ones(3)
A
Output:

As we can see in the output, the function ‘ones’ has given us a matrix of order 3 and with 1s as its elements.
Example 2:
In this example, we will use one function to create a square matrix of order 4 with all 1s as its elements.
Below are the steps that we will follow for this example:
- First, pass the required order as an argument to the one’s function.
- Display the output.
Code:
A = ones(4)[Using ones function with ‘4’ as the argument. Please note that we have passed ‘4’ as an argument because we need the matrix with 1s to be of order 4 x 4 ]
A[Displaying the output]
This is how our input and output will look like in the MATLAB command window.
Input:
A = ones(4)
A
Output:

As we can see in the output, the function ‘ones’ has given us a matrix of order 4 and with 1s as its elements.
In the above 2 examples, we have used ‘ones function’ to create a square matrix of all ones. Next, we will see the syntax to create the matrix of the required order. i.e. a matrix which is not a square matrix.
Example 3:
In this example, we will use ‘ones function’ to create a matrix with all 1s as its elements and order 2 x 3 ( 2 rows and 3 columns).
Below are the steps that we will follow for this example:
- First, pass the required order as an argument to the ones function.
- Display the output.
Code:
A = ones(2, 3)[Using ones function with ‘2’ and ‘3’ as the arguments. Please note that we have passed 2, 3 as the arguments because we need the matrix with 1s to be of order 2 x 3 ]
A[Displaying the output]
This is how our input and output will look like in the MATLAB command window.
Input:
A = ones(2, 3)
A
Output:

As we can see in the output, the function ‘ones’ has given us a matrix of order 2 x 3 and with 1s as its elements.
Example 4:
In this example, we will use ‘ones function’ to create a matrix with all 1s as its elements and order 3 x 4 ( 3 rows and 4 columns).
Below are the steps that we will follow for this example:
- First, pass the required order as an argument to the ones function.
- Display the output.
Code:
A = ones(3, 4)[Using ‘ones function’ with ‘3’ and ‘4’ as the arguments. Please note that we have passed 3, 4 as the arguments because we need the matrix with 1s to be of order 3 x 4 ]
A[Displaying the output]
This is how our input and output will look like in the MATLAB command window:
Input:
A = ones(3,4)
A
Output:

As we can see in the output, the function ‘ones’ has given us a matrix of order 3 x 4 and with 1s as its elements.