Home » Matlab » Cumulative Summation Of Vector And Matrix Elements In MatLab

Cumulative Summation Of Vector And Matrix Elements In MatLab

Cumulative summation is very important thing in data and statistical analysis. MatLab provides a command that you can do cumulative summations of different vectors, and matrices. This command is called as ‘cumsum()’.

Consider a situation that you need to calculate the cumulative summation of a number group that you took from a statistical thing. You need to just create a vector that elements of it includes your numbers. Then write this vector inside ‘cumsum()’ command.

In here, we explain how to use ‘cumsum()’ command in Matlab, with a very basic example below.

How To Use ‘cumsum()’ Command In MatLab?

>> a = cumsum([5 2 3 6])
b = cumsum([2 6 4 3; 1 6 1 6; 4 6 2 6])
c = [1 3 5;4 4 6; 4 6 3];
d = cumsum(c)

a =

     5     7    10    16


b =

     2     6     4     3
     3    12     5     9
     7    18     7    15


d =

     1     3     5
     5     7    11
     9    13    14

The use of ‘cumsum()’ command in Matlab is very simple as you see above. You just need to type the vector or matrix that you want to calculate cumulative summation of them.

In first example, we directly typed a vector inside the parantheses of ‘cumsum()’ command in Matlab. You must be aware about the situation that, the answer ‘a’ includes also 4 elements inside it. This means that, ‘cumsum()’ command calculates the cumulative summations up to each elements respectively. For example, in the cumulative calculation of 3rd element of a vector, it calculates the summation of 1st, 2nd and 3rd elements, writes the result as third element, as you see at ‘a’.

YOU CAN LEARN MatLab IN MECHANICAL BASE; Click And Start To Learn MatLab!

Also you can directly write a matrix inside ‘cumsum()’ command just like ‘b’ above. The situation for vectors is valid for matrices. As you see in results ‘c’ and ‘d’ above, cumulative calculations are done for each column elements. Think columns like vector cumulation calculations. So, a same dimension matrices are created in results of cumulative summations.

You can also directly write the created vector and matrice variables inside ‘cumsum()’ command in Matlab, as example ‘d’ above.

Do not forget to leave your comments and questions about ‘cumsum()’ command in Matlab below. Your precious feedbacks are very important for us.

Comments

One response to “Cumulative Summation Of Vector And Matrix Elements In MatLab”

Leave a Reply

Your email address will not be published. Required fields are marked *


Please

leave feedback ?

( For the post )

ASK THE MECHANICALLAND

COMMUNITY


We are always open to your feedback to improve ourselves and the quality of our content! If you have any suggestions, thoughts, or criticism, please let us know. We are trying to improve our blog with constructive feedback. We are aware of how valuable your feedback is for our future development, and we will carefully read all your comments. Thank you in advance!