如何在 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