Matlab

Floor Function in MATLAB (Round Down)

This powerful programming language provides an extensive library of functions for solving mathematical problems.

This Linux Hint article will look at floor(), a rounding function available in the MATLAB library for this operation. We will detail the structure of this function, the input and output arguments, the control flags, and the data type it accepts.

Next, we will look at the syntax of floor() with a description of how it works. After that, we will show you how to implement this function with different inputs and usage modes using some practical examples with code snippets and images.

MATLAB Floor Function Syntax

 

F = floor ( x )
F = floor( t )
F = floor ( t, unit )

 

MATLAB Floor Function Description

The MATLAB function floor() rounds the elements of the array, vector, or scalar “x” to the nearest integer with the smallest value and returns it in “F”. This rounding function accepts complex numbers in its input arguments. In these cases, the real and imaginary parts are processed separately and returned in “F”. The input argument “x” can be a scalar, a vector, a 2D matrix, or a multidimensional number. The input data types that floor() accepts are single, double, int8, int16, int32, int64, uint8, uint16, uint3, uint64, char, and logical. MATLAB’s floor() function also rounds duration matrices using the “t” input, and the unit we want to round can be specified using the “unit” input, providing great flexibility in this type of procedure of arrays. Next, we will look at some practical examples we have prepared for you, using code snippets and images to show how to use this function in different modes and with varying types of input arguments.

MATLAB Floor Function Example 1: How To Round a Scalar to the Smallest Integer Value With the Floor Function

In this example, we will look at how you can use the floor() function to round a scalar with fractions to the nearest integer value. To do this, we create scalars with random decimal values on the MATLAB command line using the rand() function, which we then enter into the “x” input argument of the floor() so that the function rounds them and displays the result.

x = 0 + (0+10)*rand(1,1)
floor ( x )

 
As we can see in the following figure, the rand() function has generated a random decimal number at “x”, and floor() has rounded this value to the nearest integer to negative infinity.

MATLAB Floor Function Example 2: How To Round Matrix and Vector to the Smallest Integer Value With the Floor Function

In this example, we’ll see how to use the floor() function to round a vector of elements with decimal fractions to the nearest integer value. To do this, we create the vector X with random decimal values in the MATLAB command line using the rand() function and pass them into the “x” input argument of the floor() so that the function rounds the values of the elements of the vector and displays the result on the screen. The output argument will be the vector “F” with the same size as “x”.

Below, we see the code snippet for this. In the following image, you can see the values of “x” and the result in “F” rounded with the floor():

x = 0 + ( 0 + 10 )*rand( 1, 10 )
floor ( x )

 
The following image shows the random vector generated by the rand() function in the MATLAB command line and the result after rounding with floor(). The method for rounding matrices is the same as for vectors.

MATLAB Floor Function Example 3: How To Round Down Complex Numbers With Nine floor() Function

The floor() function supports complex values in its input and output arguments. When we send complex numbers in “x”, floor() returns the complex value of “x” in “F” by rounding the real and complex parts separately. Next, let us look at an example where we create a vector of complex numbers with random values and round them to the nearest integer value to negative infinity using floor().

x =[ 2.3251 + 32.2532i, 12.2524 + 2.0000i, 9.9999 - 5.4478i ]
F = floor ( x )

 
The following image shows in the MATLAB command console the vector we created with the rand() function with random values, and below it is the result after rounding with floor():

MATLAB Floor Function Example 4: How To Round Duration Vector With MATLAB floor() Function

The floor() function also accepts and rounds duration arrays. This example shows you how the function works with this type of vector. We will also show you how to use the “unit” input to select the unit from which to round.

To round, this type of data, floor() has the inputs “t” and “unit”. The input argument “t” specifies the vector or matrix of durations to round, while the argument “unit” specifies the unit of time from which you want to round the values. Next, let us look at an example of rounding this data type.

The following code snippet shows a vector of random values we created in “x”. All elements of this vector have values in their time units, which we will round. Since we are only using the input “t” without specifying the units with the input “unit”, floor() will work with hours, minutes, seconds, etc.

t = hours( 10 ) + minutes( 15 : 17 ) + seconds( 1 . 47 );
t. Format = 'hh:mm:ss.SS'
floor ( t )

 

Now, we will see how to use the “unit” input to round from a specific unit of time.

t = hours( 10 ) + minutes( 15 : 17 ) + seconds( 1 . 47 );
t. Format = 'hh:mm:ss.SS'
floor ( t , 'minutes' )

 
The following image shows that the floor rounded this duration vector from the unit specified in “unit”:

Conclusion

This article showed you how to use the floor() function to round variables in MATLAB. This is one of several functions that this powerful programming language provides for this type of mathematical operation. We have examined the arguments, input, output, accepted data types, and calling modes. Also, we have prepared a working example with code snippets and images for each input type and call mode of this function to show you the different ways to use it. We hope you found this MATLAB article helpful. 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).