Ceiling in MATLAB

Have you ever needed to round numbers up to the nearest whole number or a specific decimal place in MATLAB? Look no further than the ceil() function. It is a handy tool that ensures your values are rounded up to the next highest integer. In this article, we will walk you through the basics of the MATLAB ceil() function, explaining its purpose, syntax, and how it works in simple terms.

What is the MATLAB ceil() Function?

A ceil() function in MATLAB allows us to round a decimal value toward its nearest greatest integer value. This function can be helpful when dealing with values that need to be rounded up to the closest integer or while performing mathematical calculations. This function accepts a real or complex scalar, vector, or, matrix as an input argument and returns an integer real or complex scalar, vector, or, matrix. It is worth noting that when applying ceil() to complex numbers, the function operates separately on the real and imaginary parts.

Syntax for ceil() Function

A simple syntax of the MATLAB ceil() function is given as:

y = ceil(X)

The above syntax shows that the ceil() function accepts only one parameter that can be a scalar, vector, or, matrix value. This function returns an integer value rounding towards positive infinity.

How to Use ceil() Function in MATLAB?

To illustrate the MATLAB ceil() function, consider some examples.

Example 1

This is a simple MATLAB code that uses ceil() function to round up the given decimal value toward the largest nearest integer value.

num= 5.09;


Example 2

This MATLAB code implements the ceil() function to round up the given decimal values contained in a vector toward the largest nearest integer values.

vect= [5:2.25:15];


Example 3

This MATLAB code employs the ceil() function to round up the given decimal complex values contained in a matrix towards the largest nearest integer complex values.

matr = [0.7 -0.5i 9.4; -5 -7.07+2.1i 5.4-9.6i];


Example 4

This MATLAB code first declares two vectors named vect1 and vect2 where vect1 contains positive decimal values while vect2 contains negative decimal values. Then ceil() function is used to round these two vectors towards positive infinity. After that, the vectors and their corresponding results are plotted by using the plot() command.

vect1= [5:2.25:15];
vect2= [-5:-2.25:-15];
plot(vect1, Y1)
hold on
plot(vect2, Y2)
legend('Y1','Y2', 'location', 'east');


Example 5

In this example, each value in a duration array is rounded to the nearest integer number of hours and seconds greater than or equal to that value.

time = hours(6.7) + minutes(39:41) + seconds(7.23);
time.Format = 'hh:mm:ss.SS'
Y = ceil(time)



We can round a decimal value to the closest largest integer value using the built-in MATLAB ceil() function. This function returns an integer real or complex scalar, vector, or matrix depending on the provided real or complex scalar, vector, or matrix argument. This guide explored the working of ceil() function in MATLAB by providing some easy examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.