cumprod

Cumulative product of array elements.

Category: Language Fundamentals > Operators and Elementary Operations

Syntax

B = cumprod(A)

Running product along the first non-singleton dimension.

B = cumprod(A, dim)

Running product along dimension dim.

Description

cumprod(1:5) produces the factorials 1 2 6 24 120. For a matrix it works down each column, and cumprod(A, 2) works along each row. The last element of the result equals prod of the input.

Once a NaN appears, every later entry is NaN. Integer types saturate.

Input arguments

A
Numeric or logical array.
dim
Dimension to accumulate along.

Output arguments

B
Array of the same size as A; element k is the product of the first k elements.

Example

cumprod(1:5)
A = [1 2; 3 4];
cumprod(A)
cumprod(A, 2)
Output
ans = 1×5

     1     2     6    24   120
ans = 2×2

     1     2
     3     8
ans = 2×2

     1     2
     3    12

See also

cumsum prod cummax factorial