# How to sum all the elements of a vector in Matlab and Octave

To calculate the sum of the elements of a vector use the sum() function in Matlab and Octave

**sum(v)**

The parameter v is a vector.

The function adds the values of the elements of the vector and returns the sum.

## Example

Define a vector

>> v=[1 2 3 4]

v =

1 2 3 4

Calculate the sum of the elements of the vector using the sum (v) function

>> sum(v)

ans = 10

The sum of the elements is 10

$$ 1 + 2 + 3 + 4 = 10 $$

https://how.okpedia.org/en/matlab/how-to-sum-all-the-elements-of-a-vector-in-matlab-and-octave

**Report us an error or send a suggestion to improve this page**