Updated March 13, 2023
Introduction to Matlab Round
‘Round off’ is performed in mathematics to restrict the number of significant digits. For example, the value of ‘pi’ is 3.14159265358979323, and using this entire value every time in calculations can become cumbersome and unnecessary. To avoid this, we round off the values to the required number of decimal places. This is the reason why we mostly see the value of pi as 3.14 (rounding off the value to 2 decimal places).
In MATLAB we use ‘round’ function for the purpose of rounding off the numeric values.
Syntax:
R = round (A)
R = round (A, N)
Description:
- R = round (A) will round off ‘A’ to the nearest integer. In the case of an array, all the elements of ‘A’ are rounded off to the nearest integer
- R = round (A, N) will round off ‘A’ till ‘N’ digits. If N < 0, the rounding off is done to the left of the decimal
Examples of Matlab Round
Let us now understand the code of round function in MATLAB using different examples:
Example #1
In this example, we will take a 3 x 3 matrix. For our first example, we will follow the following steps:
- Initialize the input 3 x 3 matrix
- Pass the matrix as an input to the round function
Code:
A = [3.11 4.015 11.092; -3.45 10.8 3.001; 4.981 2.012 4.1];
R = round(A)
3 4 11
-3 11 3
5 2 4]
Input:
A = [3.11 4.015 11.092; -3.45 10.8 3.001; 4.981 2.012 4.1];
R = round(A)
Output:
As we can see in the output, we have obtained the rounded off values of all the elements in the matrix.
Example #2
In this example, we will take a 2 x 2 matrix. For this example, we will follow the following steps:
- Initialize the input 2 x 2 matrix
- Pass the matrix as an input to the round function
Code:
A = [1.11 0.01; -4.45 12.803];
R = round(A)
1 0
-4 13]
Input:
A = [1.11 0.01; -4.45 12.803];
R = round(A)
Output:
As we can see in the output, we have obtained the rounded off values of all the elements in the matrix. In the above 2 examples, we saw that all the values were rounded off to the nearest integers. Next, we will learn how to round off the values to the required number of decimal places.
Example #3
In this example, we will take a 2 x 2 matrix and will round off each element to 1 decimal place. i.ethere will be one digit after the decimal in the output. For this example, we will follow the following steps:
- Initialize the input 2 x 2 matrix
- Pass the matrix as the first argument to the round function
- Pass ‘1’ as the second argument to the round function
Code:
A = [1.1134 0.09341; 4.43415 1.8103];
R = round(A, 1)
1.1 0.1
4.4 1.8]
Input:
A = [1.1134 0.09341; 4.43415 1.8103];
R = round(A,1)
Output:
As we can see in the output, all the elements in the matrix are rounded off till the first decimal place]. Next, we will learn how to round off digits to the right of the decimal point
Example #4
In this example, we will take a 2 x 2 matrix and will round it off to the one-point right of the decimal place. i.e all digits to the right of ‘tens digit’ will become zero or will be rounded off. For this example, we will follow the following steps:
- Initialize the input 2 x 2 matrix
- Pass the matrix as the first argument to the round function
- Pass ‘-1’ as the second argument to the round function
Code:
A = [321.1134 20.09341; 34.43415 14.8103];
R = round(A, -1)
320 20
30 10]
Input:
A = [321.1134 20.09341; 34.43415 14.8103];
R = round(A, -1)
Output:
As we can see in the output, all the elements in the matrix are rounded off and digits before tens place have become zero]
Conclusion
- ‘round’ function is used in MATLAB to round off the numeric values.
- ‘round’ by default rounds off values to the nearest integer. An argument can be passed if we need values to be rounded off till some other place.
Recommended Articles
This is a guide to Matlab Round. Here we also discuss the introduction and syntax of matlab round along with different examples and its code implementation. You may also have a look at the following articles to learn more –