Main Content

cumprod

Symbolic cumulative product

Description

example

B = cumprod(A) returns the cumulative product of A starting at the beginning of the first array dimension in A whose size does not equal 1. The output B has the same size as A.

  • 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 of 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.

example

B = cumprod(___,direction) specifies the direction using any of the previous syntaxes. For instance, cumprod(A,2,'reverse') returns the cumulative product within the rows of A by working from end to beginning of the second dimension.

example

B = cumprod(___,nanflag) specifies whether to include or omit NaN values from the calculation for any of the previous syntaxes. cumprod(A,'includenan') includes all NaN values in the calculation while cumprod(A,'omitnan') ignores them.

Examples

collapse all

Create a symbolic vector. Find the cumulative product of its elements.

syms x
A = (1:5)*x
A = (x2x3x4x5x)[x, 2*x, 3*x, 4*x, 5*x]

In the vector of cumulative products, element B(2) is the product of A(1) and A(2), while B(5) is the product of elements A(1) through A(5).

B = cumprod(A)
B = (x2x26x324x4120x5)[x, 2*x^2, 6*x^3, 24*x^4, 120*x^5]

Create a 3-by-3 symbolic matrix A whose all elements are x.

syms x
A = ones(3)*x
A = 

(xxxxxxxxx)[x, x, x; x, x, x; x, x, x]

Compute the cumulative product of elements of A. By default, cumprod returns the cumulative product of each column.

B = cumprod(A)
B = 

(xxxx2x2x2x3x3x3)[x, x, x; x^2, x^2, x^2; x^3, x^3, x^3]

To compute the cumulative product of each row, set the value of the dim option to 2.

B = cumprod(A,2)
B = 

(xx2x3xx2x3xx2x3)[x, x^2, x^3; x, x^2, x^3; x, x^2, x^3]

Create a 3-by-3-by-2 symbolic array.

syms x y
A(:,:,1) = [x y 0; x 3 x*y; x 1/3 y];
A(:,:,2) = [x y 3; 3 x y; y 3 x];
A
A(:,:,1) = 

(xy0x3xyx13y)[x, y, sym(0); x, sym(3), x*y; x, sym(1/3), y]

A(:,:,2) = 

(xy33xyy3x)[x, y, sym(3); sym(3), x, y; y, sym(3), x]

Compute the cumulative product along the rows by specifying dim as 2. Specify the 'reverse' option to work from right to left in each row. The result is the same size as A.

B = cumprod(A,2,'reverse')
B(:,:,1) = 

(0003x2y3xyxyxy3y3y)[sym(0), sym(0), sym(0); 3*x^2*y, 3*x*y, x*y; (x*y)/3, y/3, y]

B(:,:,2) = 

(3xy3y33xyxyy3xy3xx)[3*x*y, 3*y, sym(3); 3*x*y, x*y, y; 3*x*y, 3*x, x]

To compute the cumulative product along the third (page) dimension, specify dim as 3. Specify the 'reverse' option to work from the largest page index to the smallest page index.

B = cumprod(A,3,'reverse')
B(:,:,1) = 

(x2y203x3xxy2xy1xy)[x^2, y^2, sym(0); 3*x, 3*x, x*y^2; x*y, sym(1), x*y]

B(:,:,2) = 

(xy33xyy3x)[x, y, sym(3); sym(3), x, y; y, sym(3), x]

Create a symbolic vector containing NaN values. Compute the cumulative products.

A = [sym('a') sym('b') 1 NaN 2]
A = (ab1NaN2)[a, b, sym(1), sym(NaN), sym(2)]
B = cumprod(A)
B = (aababNaNNaN)[a, a*b, a*b, sym(NaN), sym(NaN)]

You can ignore NaN values in the cumulative product calculation using the 'omitnan' option.

B = cumprod(A,'omitnan')
B = (aababab2ab)[a, a*b, a*b, a*b, 2*a*b]

Input Arguments

collapse all

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

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 of A and returns the cumulative product of each column.

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

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

Direction of cumulation, specified as 'forward' (default) or 'reverse'.

  • 'forward' works from 1 to end of the active dimension.

  • 'reverse' works from end to 1 of the active dimension.

Data Types: char

NaN condition, specified as:

  • 'includenan' — Include NaN values from the input when computing the cumulative products, resulting in NaN values in the output.

  • 'omitnan' — Ignore all NaN values in the input. The product of elements containing NaN values is the product of all non-NaN elements. If all elements are NaN, then cumprod returns 1.

Data Types: char

Output Arguments

collapse all

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

More About

collapse 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

| | | |

Introduced in R2013b