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
% Input vector % Input vector val = -3.1; % Rounding the elements in vector Y = floor(val); % Printing the rounded vector 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
% Input vector % Input vector X = [-1.2 -0.2 -4.4 7.6 -12.0]; % Rounding the elements in vector Y = floor(X) % Printing the rounded vector 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
% Array of duration t = hours(5) + minutes(2:4) + seconds(1.78); % Format the array into time format t.Format = 'hh:mm:ss.SS' ;
% Display initial duration array disp( "duration :" )
disp(t); % Rounding the duration array 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
% Array of duration t = hours(5) + minutes(2:4) + seconds(1.78); % Format the array into time format t.Format = 'hh:mm:ss.SS' ;
% Display initial duration array disp( "duration :" )
disp(t); % Rounding the duration array to the nearest minutes % less than or equal to element 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
Article Tags :
Recommended Articles