📅  最后修改于: 2023-12-03 14:40:23.791000             🧑  作者: Mango
The cumprod
function in MATLAB calculates the cumulative product of an array along a specified dimension. It returns an array of the same size as the input, where each element is the product of all elements preceding it along the specified dimension.
B = cumprod(A)
B = cumprod(A,dim)
A
: Input arraydim
(optional): Dimension along which to calculate the cumulative product. If not provided, the default value is the first non-singleton dimension.B
: Array of the same size as A
containing the cumulative products.A = [1 2 3 4];
B = cumprod(A)
Output:
B = [1 2 6 24]
In this example, the cumulative product of the elements in array A
is calculated, resulting in [1 2 6 24]
.
A = [1 2 3; 4 5 6];
B = cumprod(A, 2)
Output:
B = [1 2 6; 4 20 120]
In this example, the cumulative product is calculated along the second dimension of array A
. So, the output B
is [1 2 6; 4 20 120]
, where the first row contains the cumulative products of the first row in A
and the second row contains the cumulative products of the second row in A
.
A = [1 2 3; 4 5 6];
B = cumprod(A, 1)
Output:
B = [1 2 3; 4 10 18]
In this example, the cumulative product is calculated along the first dimension of array A
. The output B
is [1 2 3; 4 10 18]
, where the first column remains the same as A
, and each element in the second column is the product of the corresponding elements in the first column and A
.
The cumprod
function in MATLAB is a useful tool for calculating the cumulative product along a specified dimension. It can be used to analyze trends, growth rates, and compute various statistical measures.