Cumulative Sum in MATLAB Last Updated : 18 Oct, 2021 Comments Improve Suggest changes Like Article Like Report The cumulative sum of a sequence is a running sums or partial sums of a sequence. The cumulative sums of the sequence {a,b,c,...}, are a, a+b, a+b+c, .... MATLAB allows us to calculate the cumulative sum of a vector, matrix using cumsum() method. Different syntax of cumsum() method are: B = cumsum(A)B = cumsum(A,dim)B = cumsum(___,direction)B = cumsum(___,nanflag) Let us discuss the above syntax in detail: cumsum(A)The method returns the cumulative sum of A starting at the beginning of array A.If A is a vector, then it returns the cumulative sum of sequence AIf A is a matrix, then it returns the cumulative sum along each column of A. Example 1: Matlab % Input vector A = 2:8; B = cumsum(A); % Displays cumulative sums % of A disp(B) Output : Example 2: Matlab % Input matrix A = [1 4 7; 2 5 8; 3 6 9]; disp("Matrix :") disp(A) B = cumsum(A); % Display cumulative sum of matrix A disp("Cumulative sum :") disp(B) Output : cumsum(A,dim)Returns the cumulative sum of matrix A along with each dim.dim takes two values 1 or 2.dim = 1, refers to along each column.dim = 2, refers along each row. Matlab % input matrix A = [1 3 5; 2 4 6]; disp("Matrix :") disp(A) % Cumulative sum along each % row from left to right B = cumsum(A,2); disp("Cumulative sum :") disp(B) Output : cumsum(___,direction)Returns the cumulative sum of the input vector or matrix in the given direction.direction takes two values 'forward' or 'reverse'If the direction is 'reverse', then calculates cumulative sum in reverse i.e if we are considering matrix along each column, then it returns cumulative sum starting from bottom to top of each column. Matlab % input matrix A = [1 3 5; 2 4 6]; disp("Matrix :") disp(A) % Cumulative sum of A along each % row starting from right to left B = cumsum(A,2,'reverse'); disp("Cumulative sum :") disp(B) Output : cumsum(___,nanflag)nanflag value decides whether to include or exclude NaN value of the vector in cumulative sum or not.nanflag takes two values 'includenan' or 'omitnan' corresponds to including NaN elements and excluding NaN elements respectively.'omitNaN' considers NaN values as 0. Note : NaN + number = NaN Matlab % Input vector A = [3 5 NaN 9 0 NaN]; disp("Vector :"); disp(A); % Including NaN values B = cumsum(A,'includenan'); disp("Cumulative sum Include NaN :"); disp(B); % Excluding NaN values B = cumsum(A,'omitnan'); disp("Cumulative sum Exclude NaN :"); disp(B); Output : Comment More infoAdvertise with us Next Article Cumulative vs Relative Frequency M ManikantaBandla Follow Improve Article Tags : Software Engineering MATLAB-Maths Similar Reads How to Calculate Moving Sum in MATLAB? The moving sum of a vector can be referred to as the running sum of each window of some size k in the vector. Suppose vector = [1 2 3 4 5] Then the moving sum of each window of size 2 are [1 2] = 3 [2 3] = 5 [3 4] = 7 [4 5] = 9 Then vector of moving sums is [3 5 7 9] Matlab allows us to perform this 4 min read How to Calculate Cumulative Product in MATLAB The cumulative product of a sequence is a running products or partial products of a sequence The cumulative products of the sequence {a,b,c,...}, are a, a*b, a*b*c, .... Matlab allows us to calculate the cumulative product of a vector, matrix using cumprod() method. Different syntaxes of cumprod() m 3 min read Cumulative vs Relative Frequency Cumulative frequency and relative frequency are two ways to analyze data in statistics. Cumulative frequency shows the running total of frequencies up to a certain point in a data set, while relative frequency indicates how often a particular value occurs compared to the total number of observations 4 min read M - Files in MATLAB MATLAB provides a feature to store a sequence of statements in a file and execute these statements at the MATLAB prompt exactly as if have typed each command sequentially. Such files are called M-files or script files because they contain file extensions as '.m'. M-files are basically text files whe 3 min read How to Calculate Variance in MATLAB? Pre-requisites: Calculate Variance In statistical mathematics, Variance is the measure of dispersion of a given data from its average value. This is a very important quantity in statistics and many other fields like Machine Learning and AI. Variance in MATLAB:MATLAB provides a simple function to ca 2 min read Create a cumulative histogram in Matplotlib The histogram is a graphical representation of data. We can represent any kind of numeric data in histogram format. In this article, We are going to see how to create a cumulative histogram in Matplotlib Cumulative frequency: Cumulative frequency analysis is the analysis of the frequency of occurren 2 min read Like