MATLAB sum

sum( ) command or function gives the sum value of vector or matrix or array elements, after reading this MATLAB sum topic, you will know the theory and examples.


Syntax:

sum(n)

  • n can be a vector or matrix.

Example: To create a vector and find the sum of all elements of a vector.

% create a vector n
n=[1,2,2,10,5,78] 
sum(n)

Output:

n =

1 2 2 10 5 78


ans =

98

Example: To create a matrix and find sum value of each column of the matrix.

% create the 3x3 matrix as 
n=[1,2,3;1,2,23;4,5,21] 

sum(n)

Output:

n =

1   2   3
1   2   23
4   5   21


ans =

6  9  47

Explanation:

  • 6, 9 and 47 are the sum value of the first, second and third column of the matrix respectively.

Published by

Electrical Workbook

We provide tutoring in Electrical Engineering.

Leave a Reply

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