如何在 MATLAB 中計算累積積
在本文中,我們將學習如何在 MATLAB 中計算累積積。因此,讓我們從累積積的基本定義開始。
什麼是累積積?
以累積方式計算數字序列乘積的數學運算稱為累積積。
在累積積中,計算數字序列元素的乘積直到給定索引,並將每個步驟中獲得的結果儲存為陣列的形式。
為了理解什麼是累積積?讓我們考慮以下示例。
假設一個數字序列為 [2, 4, 6, 8]。那麼,此序列的累積積將按如下方式計算
步驟 (1) - 索引 0 處的元素的累積積 = 2 = 2 = [2]。
步驟 (2) - 索引 1 處的元素的累積積 = 2 × 4 = 8 = [2, 8]。
步驟 (3) - 索引 2 處的元素的累積積 = 2 × 4 × 6 = 48 = [2, 8, 48]。
步驟 (4) - 索引 3 處的元素的累積積 = 2 × 4 × 6 × 8 = 384 = [2, 8, 48, 384]。
因此,給定序列 [2, 4, 6, 8] 的累積積為 [2, 8, 48, 384]。
累積積用於分析某個量在一段時間內的變化情況。它廣泛應用於統計學、訊號處理、金融、工程等領域。
現在,讓我們討論如何在 MATLAB 中計算累積積。
在 MATLAB 中計算累積積
MATLAB 提供了一個內建函式“cumprod”來計算數字序列的累積積。
根據不同的用例,“cumprod”函式可以具有不同的語法。以下部分描述了“cumprod”函式的不同語法及其 MATLAB 程式碼實現以計算累積積。
陣列元素的累積積
以下“cumprod”函式的語法用於計算陣列元素的累積積
CP = cumprod(A);
其中,A 是要計算其累積積的元素的輸入陣列。此函式將返回另一個與陣列“A”大小相同的陣列,並存儲在變數“CP”中。其中,陣列“CP”中的每個元素都是陣列“A”中所有元素直到給定索引的乘積。
以下 MATLAB 程式演示了此語法用於計算陣列的累積積。
示例
% MATLAB code to calculate cumulative product of an array
% Create an input array
A = [2, 4, 6, 8];
% Calculate the cumulative product
CP = cumprod(A);
% Display the input array and cumulative product array
disp('The input array is:');
disp(A);
disp('The array of cumulative product is:');
disp(CP);
輸出
The input array is:
2 4 6 8
The array of cumulative product is:
2 8 48 384
程式碼說明
在此 MATLAB 程式碼中,我們首先建立一個一維陣列“A”。然後,我們呼叫“cumprod”函式來計算陣列“A”的元素的累積積,並將結果儲存在變數“CP”中,該變數是另一個與“A”大小相同的陣列。最後,使用“disp”函式,我們顯示輸入陣列和累積積。
沿指定維度的陣列的累積積
以下“cumprod”函式的語法用於沿指定維度計算陣列的累積積
CP = cumprod(A, dim);
如果“dim = 1”,則沿陣列的列計算累積積。
如果“dim = 2”,則沿陣列的行計算累積積。
請考慮以下 MATLAB 程式以瞭解此語法的實現。
示例
% MATLAB code to calculate cumulative product of an array in a specified dimension
% Create a multidimensional input array
A = [2 4 6 8; 3 5 7 9; 1 3 5 7];
% Calculate the cumulative product along columns of the array
CP1 = cumprod(A, 1);
% Calculate the cumulative product along rows of the array
CP2 = cumprod(A, 2);
% Display the input array and cumulative product arrays
disp('The input array is:');
disp(A);
disp('The cumulative product along columns is:');
disp(CP1);
disp('The cumulative product along rows is:');
disp(CP2);
輸出
The input array is:
2 4 6 8
3 5 7 9
1 3 5 7
The cumulative product along columns is:
2 4 6 8
6 20 42 72
6 60 210 504
The cumulative product along rows is:
2 8 48 384
3 15 105 945
1 3 15 105
程式碼說明
在此 MATLAB 程式碼中,我們首先定義一個多維陣列“A”。然後,我們使用“cumprod”函式分別沿列 (dim = 1) 和行 (dim = 2) 計算此陣列的累積積。最後,我們使用“disp”函式顯示輸入陣列和累積積。
指定方向的累積積
以下“cumprod”函式的語法用於沿指定方向計算陣列的累積積
CP = cumprod(A, direction);
其中,引數 direction 可以有兩個可能的值,即“forward”和“reverse”。預設方向為 forward。
當方向設定為 forward 時,從陣列的第一個元素到最後一個元素計算累積積。當方向設定為 reverse 時,從陣列的最後一個元素到第一個元素計算累積積。
讓我們藉助示例 MATLAB 程式瞭解此語法的功能。
示例
% MATLAB code to calculate cumulative product of an array in a specified direction
% Create an input array
A = [2 4 6 8];
% Calculate the cumulative product in the forward direction
CP_F = cumprod(A, 'forward');
% Calculate the cumulative product in the reverse direction
CP_R = cumprod(A, 'reverse');
% Display the input array and cumulative product arrays
disp('The input array is:');
disp(A);
disp('The cumulative product in the forward direction is:');
disp(CP_F);
disp('The cumulative product in the reverse direction is:');
disp(CP_R);
輸出
The input array is:
2 4 6 8
The cumulative product in the forward direction is:
2 8 48 384
The cumulative product in the reverse direction is:
384 192 48 8
程式碼說明
在此 MATLAB 程式碼中,我們首先定義一個輸入陣列“A”。然後,我們使用“cumprod”函式分別沿 forward 和 reverse 方向計算此陣列的累積積,並將結果分別儲存在變數“CP_F”和“CP_R”中。最後,我們使用“disp”函式顯示輸入陣列和累積積。
包含 NaN 值的陣列的累積積
以下“cumprod”函式的語法用於計算包含 NaN 值的陣列的累積積
CP = cumprod(A, nanflag);
這裡,引數“nanflag”可以有兩個可能的值,即“omitnan”和“includenan”。
當引數 nanflag 設定為“omitnan”時,“cumprod”函式在計算累積積時會忽略陣列中存在的 NaN 值。
另一方面,如果引數 nanflag 設定為“includenan”,“cumprod”函式在計算累積積時會考慮陣列中存在的所有 NaN 值。
以下 MATLAB 產品演示了此語法的實現和執行。
示例
% MATLAB code to calculate cumulative product of an array containing NaN values
% Create an input array with NaN values
A = [2 4 6 8 NaN 3 NaN 8];
% Calculate the cumulative product with including NaN values
CP_I = cumprod(A, 'includenan');
% Calculate the cumulative product with omitting NaN values
CP_O = cumprod(A, 'omitnan');
% Display the input array and cumulative product arrays
disp('The input array is:');
disp(A);
disp('The cumulative product with including NaN values is:');
disp(CP_I);
disp('The cumulative product with omitting NaN values is:');
disp(CP_O);
輸出
The input array is:
2 4 6 8 NaN 3 NaN 8
The cumulative product with including NaN values is:
2 8 48 384 NaN NaN NaN NaN
The cumulative product with omitting NaN values is:
2 8 48 384 384 1152 1152 9216
程式碼說明
在此 MATLAB 程式碼中,我們首先定義一個輸入陣列“A”。然後,我們使用“cumprod”函式分別在包含 NaN 值和省略 NaN 值的情況下計算此陣列的累積積,並將結果分別儲存在變數“CP_I”和“CP_O”中。最後,我們使用“disp”函式顯示輸入陣列和累積積。
結論
因此,這就是使用 MATLAB 程式設計計算數字序列或陣列的累積積的全部內容。MATLAB 提供了一個內建函式“cumprod”,它具有不同的語法來計算陣列或數字序列的累積積。在本文中,我們藉助示例程式解釋了 MATLAB 中“cumprod”函式的每種語法。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP