如何在 MATLAB 中向负无穷大舍入?
向负无穷大舍入是一种将数字“N”向下舍入到小于或等于数字“N”的最近整数的方法。这种方法也称为“向下取整”。
简单来说,向负无穷大舍入一个数字就是找到小于或等于给定数字的最大整数的方法。
例如,考虑数字 4.8,当此数字向负无穷大舍入时。那么它将变为 4。这是因为 4 是小于或等于 4.8 的最大整数。
同样,如果我们有一个负数,例如 -4.7,那么它将是 -5。这是因为 -5 是小于或等于 -4.7 的最大整数。
在本教程中,让我们学习如何在 MATLAB 中将数字向负无穷大舍入。
在 MATLAB 中向负无穷大舍入
在 MATLAB 中,有一个名为“floor”的内置函数用于向负无穷大舍入。此函数可用于舍入任何数字或数字数组。
语法
“floor”函数将给定数字或数组的每个元素向下舍入到最接近的整数,朝向负无穷大。
rounded_num = floor(num);
此函数还可以具有其他语法格式。
舍入元素向量 -
rounded_vector = floor(A);
舍入持续时间数组 -
rounded_duration = floor(t);
使用指定的时间单位舍入持续时间数组 -
rounded_duration = floor(t, unit);
现在,让我们借助示例讨论这些语法格式。
将数字向负无穷大舍入
在 MATLAB 中,我们可以根据以下步骤将给定数字向负无穷大舍入 -
步骤 1 - 输入要舍入的数字。
步骤 2 - 使用“floor”函数舍入数字。
步骤 3 - 显示舍入后的数字。
示例 1
以下示例演示了上述步骤的实现。
% MATLAB code to round a number toward negative infinity
% Input the number
num = input('Please Enter a Number:');
% Round the number towards negative infinity
rounded_num = floor(num);
% Display the input and rounded numbers
disp('Input Number:');
disp(num);
disp('Rounded Number towards Negative Infinity:');
disp(rounded_num);
输出
以下是输出 -
Please Enter a Number: -4.7 Input Number: -4.7000 Rounded Number towards Negative Infinity: -5
因此,此 MATLAB 已将输入数字向负无穷大舍入。
将元素向量向负无穷大舍入
在 MATLAB 中,“floor”函数也可用于舍入元素向量。当将元素向量输入到“floor”函数时,它会将输入向量的每个元素向负无穷大舍入。
下面解释了使用 MATLAB 将元素向量向负无穷大舍入的分步过程 -
步骤 1 - 创建输入向量。
步骤 2 - 使用“floor”函数将输入向量的每个元素向负无穷大舍入。
步骤 3 - 显示舍入后的向量。
示例 2
让我们看一个示例来理解此实现。
% MATLAB code to round a vector toward negative infinity
% Create an input vector
A = [-1.3, -0.4, -7.4, 9.2, -14.7, -3.6];
% Round the vector toward negative infinity
rounded_vector = floor(A);
% Display the input and rounded vectors
disp('Input Vector:');
disp(A);
disp('Rounded Vector toward Negative Infinity:');
disp(rounded_vector);
输出
以下是输出 -
Input Vector: -1.3000 -0.4000 -7.4000 9.2000 -14.7000 -3.6000 Rounded Vector toward Negative Infinity: -2 -1 -8 9 -15 -4
将持续时间数组向负无穷大舍入
在 MATLAB 中,“floor”函数也用于将持续时间数组向负无穷大舍入。以下是将持续时间值向负无穷大舍入所涉及的步骤。
步骤 1 - 创建一个持续时间值数组。
步骤 2 - 将数组格式化为时间格式,如下所示 -
hh:mm:ss.SS
其中,“hh”表示小时,“mm”表示分钟,“ss”表示秒,“SS”表示毫秒。
步骤 3 - 使用“floor”函数舍入持续时间值数组。
步骤 4 - 显示结果。
示例 3
让我们举一个例子来理解这些步骤在 MATLAB 编程中的实现。
% MATLAB code to round a duration array
% Create an example duration array
t = [hours(3.7), minutes(-25.5), seconds(120.8)];
% Format the array in time format
t.Format = 'hh:mm:ss.SS';
% Round the duration array towards negative infinity
rounded_t = floor(t);
% Display the input and rounded duration arrays
disp('Input duration array:');
disp(t);
disp('Rounded duration array towards negative infinity:');
disp(rounded_t);
输出
运行此代码时,将产生以下输出 -
Input duration array: 03:42:00.00 -00:25:30.00 00:02:00.80 Rounded duration array towards negative infinity: 03:42:00.00 -00:25:30.00 00:02:00.00
此示例显示了如何将持续时间数组向负无穷大舍入。它以秒为单位舍入持续时间数组。
使用指定的时间单位将持续时间数组向负无穷大舍入
在 MATLAB 中,“floor”函数还提供了一种方法,可以使用指定的时间单位将持续时间数组向负无穷大舍入。
以下是在指定时间单位下舍入持续时间数组所涉及的步骤 -
步骤 1 - 创建一个持续时间数组。
步骤 2 - 将数组格式化为时间格式。
步骤 3 - 指定舍入的时间单位。它可以是“小时”、“分钟”或“秒”。
步骤 4 - 使用指定的时间单位将持续时间数组向负无穷大舍入。
步骤 5 - 显示结果。
示例 4
以下示例演示了这些步骤在 MATLAB 编程中的实现。
% MATLAB code to round a duration array with specified unit of time
% Create an example duration array
t = [hours(3.7), minutes(-25.5), seconds(120.8)];
% Format the duration array in time format
t.Format = 'hh:mm:ss.SS';
% Round the duration array towards negative infinity
rounded_t_h = floor(t, 'hours'); % Round in hours
rounded_t_m = floor(t, 'minutes'); % Round in minutes
% Display the input and rounded duration arrays
disp('Input duration array:');
disp(t);
disp('Rounded duration array towards negative infinity in hours:');
disp(rounded_t_h);
disp('Rounded duration array towards negative infinity in minutes:');
disp(rounded_t_m);
输出
以下是此 MATLAB 代码的输出 -
Input duration array:
03:42:00.00 -00:25:30.00 00:02:00.80
Rounded duration array towards negative infinity in hours:
03:00:00.00 -01:00:00.00 00:00:00.00
Rounded duration array towards negative infinity in minutes:
03:42:00.00 -00:26:00.00 00:02:00.00
此示例显示了如何使用指定的时间单位将持续时间数组向负无穷大舍入。
结论
总之,MATLAB 有一个名为“floor”的内置函数,可用于向负无穷大舍入。此函数可用于将数字、数组、持续时间值向负无穷大舍入。在本教程中,我详细解释了 MATLAB 中使用“floor”函数的所有可能的舍入操作。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP