Categories

# 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:

1. Initialize the input 3 x 3 matrix
2. 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];`[Declaring the 3 x 3 input matrix]

`R = round(A)`[Passing the input matrix to the round function] [Mathematically, if we round off all the elements in the above matrix, we will get the following values:

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:

1. Initialize the input 2 x 2 matrix
2. Pass the matrix as an input to the round function

## Code:

`A = [1.11  0.01;  -4.45  12.803];`[Declaring the 2 x 2 input matrix]

`R = round(A)`[Passing the input matrix to the round function] [Mathematically, if we round off all the elements in the above matrix, we will get the following values:

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:

1. Initialize the input 2 x 2 matrix
2. Pass the matrix as the first argument to the round function
3. Pass ‘1’ as the second argument to the round function

## Code:

`A = [1.1134  0.09341;  4.43415  1.8103];`[Declaring the 2 x 2 input matrix]

`R = round(A, 1)`[Passing the input matrix to the round function. The second argument, ‘1’ is passed to specify the number of digits we need after the decimal point] [Mathematically, if we round off all the elements in the above matrix till 1st decimal place, we will get the following values:

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:

1. Initialize the input 2 x 2 matrix
2. Pass the matrix as the first argument to the round function
3. Pass ‘-1’ as the second argument to the round function

## Code:

`A = [321.1134  20.09341;  34.43415  14.8103];`[Declaring the 2 x 2 input matrix]

`R = round(A, -1)`[Passing the input matrix to the round function. The second argument, ‘-1’ will round off all the digits before tens place] [Mathematically, if we round off all the elements in the above matrix before tens place, we will get the following values:

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]