## MATLAB round function syntax.

## MATLAB round function description and examples

The round() function rounds each element of an array or scale to the nearest value. Values less than 5 are rounded down, and values greater than or equal to 5 are rounded up. MATLAB’s round() function accepts complex numbers as input and output arguments. In this case, the real and imaginary parts are rounded separately. The input arguments and data types accepted by this function are:

d = scalar integer.

t = duration array.

unit = 'seconds', 'minutes', 'hours', 'days' and 'years'.

The data accepted by “x” are single, double, int8, int16, int32, int64, uint8, uint16, uint32, uint64, and char y logical.

With the “type” option, we can select the rounding mode with the flags “decimal” and “significant”. By default, the round() function works in “decimals” mode; that is, it rounds the decimal part of “x” after the decimal point. If the function is called with the flag ‘significant’, the rounding is done starting from the most significant integer value

MATLAB’s round() function also allows you to select the digit from which to round by sending the position number of the selected digit via the “d” input.

If the “type” input is in “decimal” mode, the digit 1 specified in “d” corresponds to the first digit after the decimal point.

If the “type” input is “significant” mode, the digit 1 specified in “d” corresponds to the first most significant digit of the integer part.

## How to round a decimal scalar to an integer with MATLAB’s round() function

In the next example, we look at the method for rounding the scalar “x” to the nearest integer value.

In this case, round() in “a” returns the rounding of “x” to the nearest integer value of 5.624577, which is 6.

## How to round complex numbers with MATLAB’s round() function

The round() function accepts complex numbers as input arguments and rounds those numbers’ real and imaginary parts. In the following example, we look at how you can round the complex scalar “x”.

## How to round the elements of a matrix with the MATLAB round() function

In the following example, we will see how to round all the elements of an array using the round() function. For this purpose, we create the array “x” with integer and fractional values.

As a result, round() returns each of the values of the elements of “x” rounded to the nearest integer.

7.1209 1.5523 7 2

0.5814 9.7690 1 10

2.6260 1.7690 3 2

0.9283 0.0170 1 0

## How to select the digit from which the rounding will be done with the round() function of MATLAB

MATLAB’s round() function lets you select the digit from which to round. This is done by sending your position using the “d” input. Rounding occurs from the position of the selected digit in “d”, starting at the decimal point or the most significant integer digit, depending on what you selected in the “type” input. In the following example, the decimal part of the scalar “x” is rounded first from digit 1, then from 2 and 3. The default “type” is sent with “decimals” so it is the same a= round( x , d, “decimals” ) as a= round(x, d)

With rounding from the digit number 2:

And with rounding from the digit number 3:

## How to select the rounding mode using the “type” input with the “decimals” and “significant” flags in the MATLAB round() function

In the previous example, we saw how to select the point from which the rounding of the fraction of “x” is performed. In the following example, we see how to round the integer part by using the “significant” flag and specifying in “d” from which point of the integer part to round.

In this case, the rounding was done from the digit 1 of the integer part of “x”, giving the following result.

Now from digit 2.

If the selected digit exceeds the decimal point, rounding will continue for the fractional part.

You can also round duration arrays using the MATLAB round() function. The input argument ‘t’ is the duration array, while the input ‘unit’ selects the time unit from which to round. These units can be ‘seconds’, ‘minutes’, ‘hours’, ‘days’ and ‘years’. In the following example, we will see how to round first the seconds, then the minutes and hours.

Rounding from the unit ‘minutes’

Rounding from the unit ‘hours’

## Conclusion

In this article, we have explained how to round integers, fractions, and complex numbers using the MATLAB round() function. Using practical examples, we have shown the different possibilities of this function. We have also explained in more detail the input type and supported data arguments, as well as the main features of this function. We hope that this MATLAB article has been useful to you. See other Linux Hint articles for more tips and information.