Open In App

How to find sum of elements of an array in MATLAB?

Improve
Improve
Like Article
Like
Save
Share
Report

This article will discuss the “Finding sum of elements of an array” in MATLAB that can be done using multiple approaches which are illustrated below.

 Using sum(A) 

This is used to return the sum of the elements of the array along the first array dimension whose size does not equal 1. It returns a row vector containing the sum of each column.

Example:

Matlab




% MATLAB code for sum(A)
% Initializing an array A
A = [1 2 3; 4 5 6]
  
% Calling the sum() function
% over the above array
Sum = sum(A)


Output:

A =
  1   2   3
  4   5   6
Sum =
  5   7   9

Using sum(A, ‘all’)

sum(A, ‘all’) is used to calculate the sum of all elements of A. And this syntax is valid only for MATLAB versions R2018b and later.

Example:

Matlab




% MATLAB code for sum(A,'all') function
% Initializing an array A
A = [1 3 5; 2 4 6; 7 9 11; 8 10 12]
  
% Calling the sum() function
% over the above array
Sum = sum(A)


Output:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
Sum =
  18   26   34

Using sum(A, dim)

sum(A, dim) is used to return the sum along dimension dim. For example, sum(A, 2) is a column vector containing the sum of each row.

Example:

Matlab




% demonstration of MATLAB sum(A,'dim')function
% Initializing an array A
A = [1 3 5; 2 4 6; 7 9 11; 8 10 12]
  
% Calling the sum() function
% over the above array along dim 2
Sum = sum(A, 2)


Output:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
Sum =
   9
  12
  27
  30

Using sum(A, vecdim)

This function is used to sum the elements of A based on the dimensions specified in the vector vecdim.

Example:

Matlab




% MATLAB code for sum(A,'vecdim')
% Initializing an array A
A = [1 3 5; 2 4 6; 7 9 11; 8 10 12; 13 14 15]
  
% Calling the sum() function
% over the above array along 
% vecdim of [2, 3]
Sum = sum(A, [2, 3])


Output:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
  13   14   15
Sum =
   9
  12
  27
  30
  42

sum(___, outtype)

sum(___, outtype) is used to return the sum with a specified data type, using any of the input arguments in the previous syntaxes. Here the outtype can be ‘default’, ‘double’, or ‘native’.

Example:

Matlab




% MATLAB code for sum(A,'outtype')
% Creating a vector of 32-Obit integers
A = int32(1:5);
  
% Calculating the above vector's
% elements by specifying the output type as native
Sum = sum(A,'native')


Output:

Sum = 15

Using sum(___, nanflag)

sum(___, nanflag) is used to specify whether to include or omit NaN values from the calculation for any of the previous syntaxes. sum(A,’includenan’) includes all NaN values in the calculation while sum(A,’omitnan’) ignores them.

Example:

Matlab




% MATLAB code for sum() with  NaN values
% Creating a vector of some numbers and NaN Values 
A = [1 -0.05 10.45 NaN 0.8 NaN 1.8 NaN];
  
% Calculating sum of the above vector
% excluding NaN values
Sum = sum(A, 'omitnan')


Output:

Sum =  14

Lets see addition of Using sum() function over sum() function. It will return Sum of the array’s elements entirely.

Example 1:

Matlab




% MATLAB code for addition of 
% Using sum() function over sum() function.
% Initializing an array A
A = [1 2 3; 4 5 6]
  
% Calling the sum() function
% over the above array
Sum = sum(sum(A))


Output:

A =
  1   2   3
  4   5   6
Sum =  21

Example 2:

Matlab




% MATLAB code for addition of 
% using sum() function over sum() function.
% Initializing an array A
A = [1 3 5; 2 4 6; 7 9 11; 8 10 12]
  
% Calling the sum() function
% over the above array
Sum = sum(sum(A))


Output:

A =
   1    3    5
   2    4    6
   7    9   11
   8   10   12
Sum =  78


Last Updated : 29 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads