MATLAB: The Language of Technical Computing |
Hello and Welcome to everyone once again. Today we would be learning how to generate cumulative sum of elements with and without using loops.
Chances are, if you didn't know what cumulative sum, loops and MATLAB are, you wouldn't be here in the first place, hence I wouldn't be taking the trouble of explaining cumulative sum or loops to you here.
But still, for an introduction
- Cumulative Sum: Quoting from mathworld.wolfram.com, "a cumulative sum is a sequence of partial sums of a given sequence. For example, the cumulative sums of the sequence {a,b,c,...}, are (a), (a+b), (a+b+c), ...."
- Loops: Loops are iterative statements. They are used when a fixed set of code needs to be executed again and again until a condition or a set of conditions are satisfied. They are of many types. In this article, we would be using the for loop.
- Running Sum/Total: "A running total is the summation of a sequence of numbers which is updated each time a new number is added to the sequence, by adding the value of the new number to the previous running total. Another term for it is partial sum."
It is just the same as Cumulative Sum, with the only difference that as compared to Cumulative Sum, where the total sum is calculated and displayed at once, in Running Sum, just as the definition says, sum is calculated in steps, every time a new new element is added to the sequence.
Cumulative Sum
To generate cumulative sum, we use the command cumsum(arguments). There are different implementations of the cumsum function depending upon its argument list. We would discuss each of those cases in detail below.
To calculate Cumulative Sum of a vector (1D Matrix)
Use the following commands:
A=[76 32 12];
display(A);
cumsum(A)
Since there is only a single dimension, the sum is calculated in a row-major order. Notice the simplicity of using the cumsum function.
To calculate Cumulative Sum of a matrix (2D Matrix)
Use the following commands:
A=[76 32 12; 23 67 22];
display(A);
cumsum(A)
Since there are two dimensions in which the cumulative sum can be found out, the cumulative sum will be calculated along the columns (default).
To explicitly tell MATLAB to perform a cumulative sum operation on a given matrix along a given order in a column major we will have to code like this
A=[76 32 12; 23 67 22];
display(A);
cumsum(A, 1)
Similarly to compute the cumulative sum row-wise, enter the following code:
A=[76 32 12; 23 67 22];
display(A);
cumsum(A, 2)
All of the above commands will produce the following outputs:
Implementing cumsum() |
Running Sum
To compute running sum, we should first be able to implement loops. In MATLAB, a for loop is implemented in the following manner:
for (range of values)
(body)
end
For better understanding, let us consider the following code:
A=[76 32 12 23 67 22];
display(A);
sum=0;
for j=1:6
sum=sum+A(j)
j=j+1;
end
In the above code as we can see, we first
You can notice at first we started with computing the cumulative sum using the cumsum function. The cumsum function gives the cumulative sum without using iterative statements (loops).
To use loops, we implemented the for loop to traverse and subsequently add our elements individually to obtain the cumulative sum of all the elements in the matrix.
And with that we finally reach the end of the post I hope you really learnt something! Stay tuned and take care :)
- Define a vector A and display it.
- Then we define a variable sum and initialize it to 0. This sum variable will be used to store the running sum we would be computing just a few steps ahead.
- Then we start our for loop. As described in the format earlier.
- The keyword for is followed by the range of values it will traverse. In our example, the length of the vector A is 6 and we are traversing all 6 elements.
- Inside the body of the for loop, we add the element in A at jth position in each iteration with sum. For this we also have to increment j at each iteration. In this manner we would continue till j becomes 6 and at that point sum will contain the total cumulative sum of all the elements.
- We hence end our loop with the keyword end.
Computing Running Sum |
You can notice at first we started with computing the cumulative sum using the cumsum function. The cumsum function gives the cumulative sum without using iterative statements (loops).
To use loops, we implemented the for loop to traverse and subsequently add our elements individually to obtain the cumulative sum of all the elements in the matrix.
And with that we finally reach the end of the post I hope you really learnt something! Stay tuned and take care :)
0 comments:
Post a Comment