How to Calculate Cumulative Product in MATLAB


In this article, we will learn how to calculate cumulative product in MATLAB. So, lets, start with the basic definition of cumulative product.

What is Cumulative Product?

A mathematical operation in which the product of a sequence of numbers is computed in a cumulative manner is called cumulative product.

In the cumulative product, the product of elements of a sequence of numbers is computed up to the given index and the result obtained in each step is stored in the form of an array.

To understand the what is the cumulative product? Let’s consider an example as follows.

Let a sequence of numbers as [2, 4, 6, 8]. Then, the cumulative product of this sequence will be calculated as follows:

Step (1) − Cumulative product of elements at index 0 = 2 = 2 = [2].

Step (2) − Cumulative product of elements at index 1 = 2 × 4 = 8 = [2, 8].

Step (3) − Cumulative product of elements at index 2 = 2 × 4 × 6 = 48 = [2, 8, 48].

Step (4) − Cumulative product of elements at index 3 = 2 × 4 × 6 × 8 = 384 = [2, 8, 48, 384].

Therefore, the cumulative product of the given sequence [2, 4, 6, 8] is [2, 8, 48, 384].

The cumulative product is used to analyze how a quantity changes over a certain period of time. It is widely used in the field of statistics, signals processing, finance, engineering, and more.

Now, let us discuss how to calculate cumulative product in MATLAB.

Calculate Cumulative Product in MATLAB

MATLAB provides a built−in function ‘cumprod’ to compute the cumulative product of a sequence of numbers.

The ‘cumprod’ function can have different syntaxes based on different use cases. The following sections describe different syntaxes of the ‘cumprod’ function and their MATLAB code implementation to calculate the cumulative product.

Cumulative Product of Elements of an Array

The following syntax of the ‘cumprod’ function is used to calculate the cumulative product of the elements of an array:

CP = cumprod(A);

Where, A is an input array of elements whose cumulative product to be calculated. This function will return another array of the same size as the array ‘A’ and is stored in the variable ‘CP’. Where, each element in the array ‘CP’ is the product of all elements in the array ‘A’ up to the given index.

The following MATLAB program demonstrates the use of this syntax to calculate the cumulative product of an array.

Example

% MATLAB code to calculate cumulative product of an array
% Create an input array
A = [2, 4, 6, 8];

% Calculate the cumulative product
CP = cumprod(A);

% Display the input array and cumulative product array
disp('The input array is:');
disp(A);
disp('The array of cumulative product is:');
disp(CP);

Output

The input array is:
   2   4   6   8
The array of cumulative product is:
     2     8    48   384

Code Explanation

In this MATLAB code, we start by creating one−dimensional array ‘A’. Then, we call the ‘cumprod’ function to calculate the cumulative product of elements of the array ‘A’ and store the result in the variable ‘CP’, which is another array of the same size as ‘A’. Finally, using the ‘disp’ function, we display the input array and the cumulative product.

Cumulative Product of an Array along a Specified Dimension

The following syntax of the ‘cumprod’ function is used to calculate the cumulative product of an array along a specified dimension:

CP = cumprod(A, dim);

If ‘dim = 1’, the cumulative product will be calculated along the columns of the array.

If ‘dim = 2’, the cumulative product will be calculated along the rows of the array.

Consider the following MATLAB program to understand the implementation of this syntax.

Example

% MATLAB code to calculate cumulative product of an array in a specified dimension
% Create a multidimensional input array
A = [2 4 6 8; 3 5 7 9; 1 3 5 7];

% Calculate the cumulative product along columns of the array
CP1 = cumprod(A, 1);

% Calculate the cumulative product along rows of the array
CP2 = cumprod(A, 2);

% Display the input array and cumulative product arrays
disp('The input array is:');
disp(A);
disp('The cumulative product along columns is:');
disp(CP1);
disp('The cumulative product along rows is:');
disp(CP2);

Output

The input array is:
   2   4   6   8
   3   5   7   9
   1   3   5   7
The cumulative product along columns is:
     2     4     6     8
     6    20    42    72
     6    60   210   504
The cumulative product along rows is:
     2     8    48   384
     3    15   105   945
     1     3    15   105

Code Explanation

In this MATLAB code, we start by defining a multidimensional array ‘A’. Then, we use the ‘cumprod’ function to calculate the cumulative product of this array along the columns (dim = 1) and rows (dim = 2) respectively. Finally, we use the ‘disp’ function to display the input array and the cumulative products.

Cumulative Product in a Specified Direction

The following syntax of the ‘cumprod’ function is used to calculate the cumulative product of an array in a specified direction:

CP = cumprod(A, direction);

Where, the parameter direction can have two possible values, i.e. ‘forward’ and ‘reverse’. The default direction is forward.

When the direction is set to forward, the cumulative product is calculated from the first to the last element of the array. When the direction is set to reverse, the cumulative product is calculated from the last to the first element of the array.

Let us understand the function of this syntax with the help of an example MATLAB program.

Example

% MATLAB code to calculate cumulative product of an array in a specified direction
% Create an input array
A = [2 4 6 8];

% Calculate the cumulative product in the forward direction
CP_F = cumprod(A, 'forward');

% Calculate the cumulative product in the reverse direction
CP_R = cumprod(A, 'reverse');

% Display the input array and cumulative product arrays
disp('The input array is:');
disp(A);
disp('The cumulative product in the forward direction is:');
disp(CP_F);
disp('The cumulative product in the reverse direction is:');
disp(CP_R);

Output

The input array is:
     2     4     6     8

The cumulative product in the forward direction is:
     2     8    48   384

The cumulative product in the reverse direction is:
   384   192    48     8

Code Explanation

In this MATLAB code, we start by defining an input array ‘A’. Then, we use the ‘cumprod’ function to calculate the cumulative product of this array in the forward and reverse directions and store the results in variables ‘CP_F’ and ‘CP_R’ respectively. Finally, we use the ‘disp’ function to display the input array and the cumulative products.

Cumulative Product of Array containing NaN Values

The following syntax of the ‘cumprod’ function is used to calculate the cumulative product an array containing NaN values:

CP = cumprod(A, nanflag);

Here, the parameter ‘nanflag’ can have two possible values, i.e. ‘omitnan’ and ‘includenan’.

When the parameter nanflag is set to ‘omitnan’, the ‘cumprod’ function ignores the NaN values present in the array while computing the cumulative product.

On the other hand, if the parameter nanflag is set to ‘includenan’, the ‘cumprod’ function considers all the NaN values present in the array while calculating the cumulative product.

The following MATLAB product demonstrate the implementation and execution of this syntax.

Example

% MATLAB code to calculate cumulative product of an array containing NaN values
% Create an input array with NaN values
A = [2 4 6 8 NaN 3 NaN 8];

% Calculate the cumulative product with including NaN values
CP_I = cumprod(A, 'includenan');

% Calculate the cumulative product with omitting NaN values
CP_O = cumprod(A, 'omitnan');

% Display the input array and cumulative product arrays
disp('The input array is:');
disp(A);
disp('The cumulative product with including NaN values is:');
disp(CP_I);
disp('The cumulative product with omitting NaN values is:');
disp(CP_O);

Output

The input array is:
     2     4     6     8   NaN     3   NaN     8

The cumulative product with including NaN values is:
     2     8    48   384   NaN   NaN   NaN   NaN

The cumulative product with omitting NaN values is:
           2           8          48         384         384        1152        1152        9216

Code Explanation

In this MATLAB code, we start by defining an input array ‘A’. Then, we use the ‘cumprod’ function to calculate the cumulative product of this array with including NaN values and omitting NaN values and store the results in variables ‘CP_I’ and ‘CP_O’ respectively. Finally, we use the ‘disp’ function to display the input array and the cumulative products.

Conclusion

Hence, this is all about calculating cumulative product of a sequence of numbers or an array using MATLAB programming. MATLAB provides a built−in function ‘cumprod’ with different syntaxes to compute the cumulative product of an array or a sequence of numbers. In this article, we have explained each syntax of the ‘cumprod’ function in MATLAB with the help of example programs.

Updated on: 07-Aug-2023

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements