Accelerating the pace of engineering and science

# Documentation Center

• Trials
• Product Updates

# cumprod

Cumulative product

## Description

example

B = cumprod(A) returns an array the same size as the array A containing the cumulative product.

• If A is a vector, then cumprod(A) returns a vector containing the cumulative product of the elements of A.

• If A is a matrix, then cumprod(A) returns a matrix containing the cumulative products for each column of A.

• If A is a multidimensional array, then cumprod(A) acts along the first nonsingleton dimension.

example

B = cumprod(A,dim) returns the cumulative product along dimension dim. For example, if A is a matrix, then cumprod(A,2) returns the cumulative product of each row.

## Examples

expand all

### Cumulative Product of Vector

Find the cumulative product of the integers from 1 to 5.

```A = [1:5];
B = cumprod(A)```
```B =

1     2     6    24   120```

B(2) is the product of A(1) and A(2), while B(5) is the product of elements A(1) through A(5).

### Cumulative Product of Each Column in Matrix

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

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

1     4     7
2     5     8
3     6     9```

Find the cumulative product of the columns of A.

`B = cumprod(A)`
```B =

1     4     7
2    20    56
6   120   504```

B(5) is the product of A(4) and A(5), while B(9) is the product of A(7) , A(8), and A(9).

### Cumulative Product of Each Row in Matrix

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

`A = [1 3 5; 2 4 6]`
```A =

1     3     5
2     4     6```

Find the cumulative product of the rows of A.

`B = cumprod(A,2)`
```B =

1     3    15
2     8    48```

B(3) is the product of A(1) and A(3), while B(5) is the product of A(1), A(3), and A(5) .

### Logical Input with Double Output

Create an array of logical values.

`A = [true false true; true true false]`
```A =

1     0     1
1     1     0
```

Find the cumulative product of the rows of A.

`B = cumprod(A,2)`
```B =

1     0     0
1     1     0```

The output is double.

`class(B)`
```ans =

double```

## Input Arguments

expand all

### A — Input arrayvector | matrix | multidimensional array

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

### dim — Dimension to operate alongpositive integer scalar

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

Consider a two-dimensional input array, A.

• cumprod(A,1) works along the rows of A and returns the cumulative product of each column.

• cumprod(A,2) works along the columns of A and returns the cumulative product of each row.

cumprod returns A if dim is greater than ndims(A).

Data Types: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

## Output Arguments

expand all

### B — Cumulative product arrayvector | matrix | multidimensional array

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

The class of B is the same as the class of A except if A is logical, in which case B is double.

## More About

expand all

### First Nonsingleton Dimension

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

For example:

• If X is a 1-by-n row vector, then the second dimension is the first nonsingleton dimension of X.

• If X is a 1-by-0-by-n empty array, then the second dimension is the first nonsingleton dimension of X.

• If X is a 1-by-1-by-3 array, then the third dimension is the first nonsingleton dimension of X.

## See Also

Was this topic helpful?