cumprodCumulative product
cumprod(A)A is a numeric or character array.A.A.cumprod(A, dim)A is a numeric or character array.dim should be a positive integer scalar, not equal to inf nor nan.dim-th dimension of A.size(A, dim) == 1 or dim > ndims(A), then the output is the same as A.cumprod(A, option)A is a numeric or character array.option should be either 'reverse', 'forward', 'includenan' or 'omitnan'.NaN in the operation.cumprod(A, dim, option)cumprod(A, dim) above. In addition, an option is specified for controlling the operation direction or inclusion of NaN in the operation.cumprod(A, option1, option2)cumprod(A, option) above. In addition, a second option is specified.option2 will override option1 if they contradict with each other.cumprod(A, dim, option1, option2)cumprod(A, dim, option) above. In addition, a second option is specified.option2 will override option1 if they contradict with each other.| Option | Meaning | Default |
|---|---|---|
'forward' | The cumulative products are obtained in the forward direction. | Default for cumsum, cumprod, cummin, cummax |
'reverse' | The cumulative products are obtained in the reverse direction. | Not default for cumsum, cumprod, cummin and cummax |
'includenan' | Include NaN in the calculation | Default for cumsum, cumprod |
'omitnan' | Omit NaN in the calculation | Default for cummax, cummin |
A is the first dimension. The product operation is performed along the vertical direction. For instance, s(:,1) contains cumulative products of elements of A(:,1).% Matrix of size [3,4]
a = reshape(1:12,3,4)
% Cumulative products
cumprod(a)
a =
1.0000 4.0000 7.0000 10.000
2.0000 5.0000 8.0000 11.000
3.0000 6.0000 9.0000 12.000
ans =
1.0000 4.0000 7.0000 10.000
2.0000 20.000 56.000 110.00
6.0000 120.00 504.00 1320.0
p(1,:) contains cumulative products of elements of A(1,:).% Matrix of size [3,4]
A = reshape(1:12,3,4)
% Cumulative products along 2nd dimension
p = cumprod(A,2)
A =
1.0000 4.0000 7.0000 10.000
2.0000 5.0000 8.0000 11.000
3.0000 6.0000 9.0000 12.000
p =
1.0000 4.0000 28.000 280.00
2.0000 10.000 80.000 880.00
3.0000 18.000 162.00 1944.0
cumprod by default includes NaN, therefore NaN appears in the outputs of the 1st and 2nd calls of cumprod. In fact, multiplication with NaN always returns NaN. In the 3rd call of cumprod, NaN is ignored in the multiplication operation. a = [1:5 nan 6]
% Default options
disp('Include nan, forward')
cumprod(a)
% Include nan, forward direction
disp('Include nan, forward.')
cumprod(a, 'includenan')
% Omit NaN, forward
disp('Omit nan, forward')
cumprod(a, 'omitnan')
% Include nan, forward
disp('Include nan, forward')
cumprod(a, 'forward')
% Include nan, reverse direction
disp('Include nan, reverse')
cumprod(a, 'reverse')
a =
1.0000 2.0000 3.0000 4.0000 5.0000 NaN 6.0000
Include nan, forward
ans =
1.0000 2.0000 6.0000 24.000 120.00 NaN NaN
Include nan, forward.
ans =
1.0000 2.0000 6.0000 24.000 120.00 NaN NaN
Omit nan, forward
ans =
1.0000 2.0000 6.0000 24.000 120.00 120.00 720.00
Include nan, forward
ans =
1.0000 2.0000 6.0000 24.000 120.00 NaN NaN
Include nan, reverse
ans =
NaN NaN NaN NaN NaN NaN 6.0000