How to Round toward negative infinity in MATLAB
Last Updated :
06 Jul, 2021
Rounding a number towards infinity means rounding the number X to the nearest integer less than or equal to X. In this article, we will discuss how to round toward negative infinity in MATLAB.
Example:
Suppose X = 3.5, then result is 3
And if X = -3.5, then result is -4
The floor() function in MATLAB can be leveraged to round the number towards negative infinity. Different syntaxes of floor() function in Matlab are
- Y = floor(val)
- Y = floor(X)
- Y = floor(t)
- Y = floor(t,unit)
Now we will discuss the syntaxes of the above function:
Y = floor(val):
- Rounds the element val to the nearest integer less than or equal to val.
Matlab
val = -3.1;
Y = floor(val);
disp(Y)
|
Output :
-4
Y = floor(X):
- The function takes input as a vector of elements X.
- Returns the vector by rounding each element in X toward negative infinity.
Matlab
X = [-1.2 -0.2 -4.4 7.6 -12.0];
Y = floor(X)
disp(Y)
|
Output :
-2 -1 -5 7 -12
Y = floor(t):
- Here t is an array of duration in “hh:mm:ss:SS”
- hh: Hours
- mm: Minutes
- ss: Seconds
- SS: Milliseconds
- Rounds every element of array t to its nearest number of seconds less than or equal to the element.
Matlab
t = hours(5) + minutes(2:4) + seconds(1.78);
t.Format = 'hh:mm:ss.SS' ;
disp( "duration :" )
disp(t);
Y1 = floor(t);
disp( "Rounded duration :" );
disp(Y1);
|
Output :
duration :
05:02:01.78 05:03:01.78 05:04:01.78
Rounded duration :
05:02:01.00 05:03:01.00 05:04:01.00
Y = floor(t,unit):
- Here t is the duration array where every element in the format of “hh:mm:ss:SS”
- Rounds each element of t to the nearest number of the specified unit of time less than or equal to that element.
- Unit of time can be ‘seconds’, ‘minutes’, ‘hours’, ‘days’, or ‘years’.
- The default value of unit is ‘seconds’.
Matlab
t = hours(5) + minutes(2:4) + seconds(1.78);
t.Format = 'hh:mm:ss.SS' ;
disp( "duration :" )
disp(t);
Y1 = floor(t, 'minutes' );
disp( "Rounded duration :" );
disp(Y1);
|
Output :
duration :
05:02:01.78 05:03:01.78 05:04:01.78
Rounded duration :
05:02:00.00 05:03:00.00 05:04:00.00
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...