How to find sum of elements of an array in MATLAB?
Last Updated :
29 Jul, 2021
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
Similar Reads
How to Find Index of Element in Array in MATLAB? In MATLAB, the arrays are used to represent the information and data. You can use indexing to access the elements of the array. Â In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indi
4 min read
How to Find Indices and Values of Nonzero Elements in MATLAB? MATLAB provides functionality that finds the indices and values of all non-zero elements in a vector or multidimensional array; the find() function. The find function with required parameters gives back a vector containing indices of non-zero elements in the passed array/vector. Syntax: vec = [] %so
3 min read
How to Find the Position of a Number in an Array in MATLAB? Finding the position of a number in an array, which can be done using the find() function. The find() function is used to find the indices and values of the specified nonzero elements. Syntaxfind(X) Parameters: This function accepts a parameter. X: This is the specified number whose position is goin
1 min read
How to Find Number of Function Arguments in MATLAB? The number of function arguments passed in MATLAB will be determined in the following article. Unlike C, C++, and Java, MATLAB can accommodate a variable amount of parameters provided into a function without throwing an error. We'll go through how we determine the actual amount of parameters supplie
4 min read
How to extract numbers from cell array in MATLAB? In this article, we are going to discuss the extraction of numbers from the cell array with the help of regexp(), str2double(), cat(), and isletter() functions. What is a Cell Array? A cell array is nothing but a data type having indexed data containers called cells, where each cell contains any typ
3 min read
How to Calculate Moving Sum in MATLAB? The moving sum of a vector can be referred to as the running sum of each window of some size k in the vector. Suppose vector = [1 2 3 4 5] Then the moving sum of each window of size 2 are [1 2] = 3 [2 3] = 5 [3 4] = 7 [4 5] = 9 Then vector of moving sums is [3 5 7 9] Matlab allows us to perform this
4 min read
How to Create a Matrix From a Nested Loop in MATLAB? Matrices are 2-dimensional arrays that store numeric or symbolic data. It is convenient to create them with the help of nested loops as the outer loop creates the elements along one dimension and the inner loop creates the elements along the second dimension. In this article, we will see how to crea
2 min read
How to detect duplicate values and its indices within an array in MATLAB? In this article, we will discuss how to find duplicate values and their indices within an array in MATLAB. It can be done using unique(), length(), setdiff(), and numel() functions that are illustrated below: Using Unique() Unique(A) function is used to return the same data as in the specified array
3 min read
Find Index of Cells Containing My String in MATLAB Cell Arrays in MATLAB are a type of array that store data in the form of cells. The benefit of using these cell arrays is that they can store data of different types as cells within a cell array only. In this article, we will see how to find a given string in a cell array. Firstly, we shall check wh
2 min read
How to Iterate through each element in N-Dimensional matrix in MATLAB? In MATLAB, a matrix is considered a two-dimensional array of numbers. Other programming languages work with numbers but in MATLAB, every number is a matrix or array. To create a matrix in MATLAB, numbers are entered in each row by adding a comma or space and the ending of each row is marked by a sem
4 min read