Matlab

round function in MatLab

The following article explains how to round integers, fractions, and complex numbers using the MATLABĀ® round() function. In this article, we use practical examples to show you how to use this function in its various rounding modes. We also detail the main functions of round() and the data types supported for each input.

MATLAB round function syntax.

a = round(x)

a = round(x, d)

a = round(x, d, type)

a = round(t)

a = round(t, unit)

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:

x = scalar, vector, matrix and multidimensional array.

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.

x = 5.624577;

a = round (x)

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”.

x= 1.5811 + 0.7071i

a = round(x)

a = 1.6000 + 0.7000i

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.

x = [ 7.1209, 1.5523 ; 0.5814, 9.7690 ;

2.6260, 1.7690 ; 0.9283, 0.0170 ]

a = round(x)
,

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

x = a =

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)

x = 351.2345

a = round ( x, 1)

a = 351.2000

With rounding from the digit number 2:

a = round ( x, 2)

a = 1.2300

And with rounding from the digit number 3:

a = round ( x, 3)

a = 1.2340

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.

x = 351<strong>.</strong>2345;

a = round ( x, 1, "significant")

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

a = 400

Now from digit 2.

a = round ( x, 2, "significant")
a = 350

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

a = round ( x, 3, "significant")
a = 351

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.

t = hours(6) + minutes(15) + seconds(58);
t.Format = 'hh:mm:ss';
a = round (t,'seconds')
a = 06:15:58.00

Rounding from the unit ‘minutes’

a = round (t,'minutes')
a = 06:16:00.00

Rounding from the unit ‘hours’

a = round (t,'hours')
a = 06:00:00.00

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.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).