# cumprod

Cumulative product

### p = cumprod(A)

• A is an array of any number of dimensions.
• The output argument p contains the cumulative products and has the same size as A.
• The cumulative products are obtained from vectors along the first non-singleton dimension of A.

Example 1: In the following, the first non-singleton dimension of 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.000   4.000   7.000   10.00
2.000   5.000   8.000   11.00
3.000   6.000   9.000   12.00

ans =
1.000   4.000   7.000   10.00
2.000   20.00   56.00   110.0
6.000   120.0   504.0   1320.


### p = cumprod(A, dim)

• dim should be a positive integer scalar, not equal to inf or nan.
• It obtains the cumulative products along the dim-th dimension of A.
• If size(A,dim) == 1 or dim > ndims(A), then p is the same as A.

Example 2: In the following, The product operation is performed along the horizontal direction (2nd dimension). For instance, 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.000   4.000   7.000   10.00
2.000   5.000   8.000   11.00
3.000   6.000   9.000   12.00

p =
1.000   4.000   28.00   280.0
2.000   10.00   80.00   880.0
3.000   18.00   162.0   1944.


### p = cumprod(A, option)

• option should be either 'reverse', 'forward', 'includenan' or 'omitnan'.
• These options control the operation direction, or whether or not NaN should be included in the operation. For details, see Tables 1 and 2 below.

Note

By default, cummin and cummax omit NaN, whereas cumsum and cumprod include NaN.

Example 3: Cumulative products of the same vector but with different options.

a=[1:5 nan 6]
% Default options
cumprod(a)
% Include nan, forward direction
cumprod(a, 'includenan')
% Omit nan, forward direction
cumprod(a, 'omitnan')
% Forward direction, include nan
cumprod(a, 'forward')
% Reverse direction, include nan
cumprod(a, 'reverse')

a =
1.000   2.000   3.000   4.000   5.000    nan   6.000

ans =
1.000   2.000   6.000   24.00   120.0    nan    nan

ans =
1.000   2.000   6.000   24.00   120.0    nan    nan

ans =
1.000   2.000   6.000   24.00   120.0   120.0   720.0

ans =
1.000   2.000   6.000   24.00   120.0    nan    nan

ans =
nan    nan    nan    nan    nan    nan   6.000


Table 1: Options for operation direction.

Option value Meaning Default
'forward' The cumulative products are obtained in the forward direction. YES
'reverse' The cumulative products are obtained in the reverse direction. NO

Table 2: Options for including or omitting NaN.

Option value Meaning Default
'includenan' Include NaN in the operation YES
'omitnan' Omit NaN in the opeartion NO

### p = cumprod(A, dim, option)

• Same as p = cumprod(A, dim) above, except that an option can be specified for controlling the operation direction or whether or not NaN should be included.
• See Tables 1 and 2 above for available options.

### p = cumprod(A, option1, option2)

• Same as p = cumprod(A, option) above, except that two options can be specified at the same time.
• option2 will overwrite option1 if they contradict each other.
• See Tables 1 and 2 above for available options.

### p = cumprod(A, dim, option1, option2)

• Same as p = cumprod(A, dim, option) above, except that two options can be specified at the same time.
• option2 will overwrite option1 if they contradict each other.
• See Tables 1 and 2 above for available options.