Main Content

cumprod

Cumulative product

Description

example

B= cumprod(A)returns the cumulative product ofAstarting at the beginning of the first array dimension inAwhose size does not equal 1.

  • IfAis a vector, thencumprod(A)returns a vector containing the cumulative product of the elements ofA.

  • IfAis a matrix, thencumprod(A)returns a matrix containing the cumulative products for each column ofA.

  • IfAis a multidimensional array, thencumprod(A)acts along thefirst nonsingleton dimension.

example

B= cumprod(A,dim)returns the cumulative product along dimensiondim. For example, ifAis a matrix, thencumprod(A,2)returns the cumulative product of each row.

example

B= cumprod(___,direction)optionally specifies the direction using any of the previous syntaxes. You must specifyA, and optionally can specifydim. For instance,cumprod(A,2,'reverse')returns the cumulative product within the rows ofAby working from end to beginning of the second dimension.

example

B= cumprod(___,nanflag)specifies whether to include or omitNaNvalues from the calculation for any of the previous syntaxes.cumprod(A,'includenan')includesNaNvalues in the calculation whilecumprod(A,'omitnan')ignores them.

Examples

collapse all

Find the cumulative product of the integers from1to5. The elementB(2)is the product ofA(1)andA(2), whileB(5)is the product of elementsA(1)throughA(5).

A = 1:5; B = cumprod(A)
B =1×51 2 6 24 120

Define a 3-by-3 matrix whose elements correspond to their linear indices.

A = [1 4 7; 2 5 8; 3 6 9]
A =3×31 4 7 2 5 8 3 6 9

Find the cumulative product of the columns ofA. The elementB(5)is the product ofA(4)andA(5), whileB(9)is the product ofA(7),A(8), andA(9).

B = cumprod(A)
B =3×31 4 7 2 20 56 6 120 504

Define a 2-by-3 matrix whose elements correspond to their linear indices.

A = [1 3 5; 2 4 6]
A =2×31 3 5 2 4 6

Find the cumulative product of the rows ofA. The elementB(3)is the product ofA(1)andA(3), whileB(5)is the product ofA(1),A(3), andA(5).

B = cumprod(A,2)
B =2×31 3 15 2 8 48

Create an array of logical values.

A = [true false true; true true false]
A =2x3 logical array1 0 1 1 1 0

Find the cumulative product of the rows ofA.

B = cumprod(A,2)
B =2×31 0 0 1 1 0

The output has typedouble.

class(B)
ans = 'double'

Create a 3-by-3 matrix of random integers between 1 and 10.

rngdefault; A = randi([1,10],3)
A =3×39 10 3 10 7 6 2 1 10

Calculate the cumulative product along the columns. Specify the“反向”option to work from bottom to top in each column. The result is the same size asA.

B = cumprod(A,“反向”)
B =3×3180 70 180 20 7 60 2 1 10

Create a vector containingNaNvalues and compute the cumulative products. By default,cumprodincludesNaNvalues. When you includeNaNvalues in the calculation, the cumulative product becomesNaNas soon as the firstNaNvalue inAis encountered.

A = [1 3 NaN 2 4 NaN]; B = cumprod(A)
B =1×61 3 NaN NaN NaN NaN

IgnoreNaNvalues in the cumulative product calculation using the'omitnan'option.

B = cumprod(A,'omitnan')
B =1×61 3 3 6 24 24

Input Arguments

collapse all

Input array, specified as a vector, matrix, or multidimensional array.

Data Types:double|single|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical
Complex Number Support:Yes

Dimension to operate along, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1.

Consider a two-dimensional input array,A.

  • cumprod(A,1)works on successive elements in the columns ofAand returns the cumulative products of each column.

  • cumprod(A,2)works on successive elements in the rows ofAand returns the cumulative products of each row.

cumprodreturnsAifdimis greater thanndims(A).

Direction of cumulation, specified as'forward'(default) or“反向”.

  • 'forward'works from1toendof the active dimension.

  • “反向”works fromendto1of the active dimension.

NaNcondition, specified as one of these values:

  • “includenan'— IncludeNaNvalues from the input when computing each product, resulting inNaNvalues in the output.

  • 'omitnan'— IgnoreNaNvalues in the input. The product of elements containingNaNvalues is the product of all non-NaNelements. If all elements areNaN, thencumprodreturns 1.

Output Arguments

collapse all

Cumulative product array, returned as a vector, matrix, or multidimensional array of the same size as the input arrayA.

The class ofBis the same as the class ofAexcept ifAislogical, in which caseBisdouble.

More About

collapse all

First Nonsingleton Dimension

The first nonsingleton dimension is the first dimension of an array whose size is not equal to1.

For example:

  • IfXis a 1-by-n row vector, then the second dimension is the first nonsingleton dimension ofX.

  • IfXis a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension ofX.

  • IfX是1-by-1-by-3数组,那么第三维吗s the first nonsingleton dimension ofX.

Tips

  • Many cumulative functions in MATLAB®support the“反向”option. This option allows quick directional calculations without needing a flip or reflection of the input array.

Extended Capabilities

Introduced before R2006a