Ceiling in MATLAB (Round Up)

MATLAB provides an extensive library of functions for solving mathematical problems of all kinds.

In this Linux Hint article, we look at how to use the ceil() function, one of the rounding functions available in the MATLAB library for this type of operation. We will explain the structure of this function, the input and output arguments, the control flags, and the data type it accepts.

Next, we look at the syntax of ceil() and describe how it works. Then, using practical examples with code snippets and images, we show you how to use this function with different input types and usage modes.

MATLAB ceil function syntax

F = ceil ( x )

F = ceil( t )

F = ceil ( t, unit )

MATLAB ceil function description

The MATLAB function ceil() rounds the elements of the matrix, vector, or scalar “x” to the nearest integer with the largest value and returns it in “F”. This rounding function accepts complex numbers as input arguments. In this case, the real and imaginary parts are processed separately and returned in “F”. The input argument “x” can be a scalar, a vector, a 2D array, or a multidimensional number. The input data types that ceil() accepts are single, double, int8, int16, int32, int64, uint8, uint16, uint3, uint64, char, and logical. The MATLAB ceil() function also rounds duration arrays using the input “t”, and the unit to round to can be specified using the input “unit”. Here are some practical examples we have prepared for you. Using code snippets and pictures, we will show you how to use this function in different modes and with different types of input arguments.

MATLAB Ceil Function Example 1: Round a scalar value to the largest integer value using the ceil() function.

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

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

ceil ( x )

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

MATLAB ceil() function Example 2: How to round a matrix and a vector to the largest integer value with the ceil function.

In this example, we will see how to use the ceil() function to round a vector of elements with decimal fractions to the nearest integer value to positive infinity. To accomplish this, in the MATLAB command line, we create the vector X with random decimal values and pass them to the input argument “x” of ceil(), which rounds the values of the elements of the vector and displays the result on the screen. The output argument is the vector “F” with the same size as “x”.

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

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

F = ceil ( x )

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

MATLAB function ceil Example 3: Rounding up complex numbers with the function ceil().

The ceil() function also supports complex values in its input and output arguments. When we send complex numbers in “x”, ceil() 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 at positive infinity using ceil().

x = [ 7.3541 + 12.2332i, 2.1484 + 2.0250i, 1.9999 - 5.1938i ]

F = ceil ( x )

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

MATLAB ceil function Example 4: Rounding the duration vector with the MATLAB ceil() function

The ceil() function also accepts and rounds duration arrays. In this example, we show you how the function works with these types of vectors and matrices. We will also show you how to use the “Unit” input to select the unit from which to round.

To round this data type, cei() 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 to round the values. Next, let us look at an example of rounding this type of data.

The following code snippet shows a vector of random values that we create at “x”. All elements of this vector have values in their time units, which we will round off. Since we only use the input “t” without specifying the units with the input “unit”, ceil() works with hours, minutes, seconds, etc.

t = hours( 9 ) + minutes( 11 : 15 ) + seconds( 1.47 );

t. Format = 'hh:mm:ss.SS'

F = ceil ( t )

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

t = hours( 9 ) + minutes( 11 : 15 ) + seconds( 1.47 );

t. Format = 'hh:mm:ss.SS'

F = ceil ( t, 'minutes' )

As seen in the following image, ceil() rounded this duration vector from the unit specified in ” unit”, in this case, minutes.


In this article, we show you how to use the ceil() 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 discussed arguments, input, output, accepted data types, and calling modes. We have also prepared for you 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 have found this MATLAB article useful. Check out 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).