How to Sum Elements of a Matrix in MATLAB

Ammar Ali Feb 02, 2024
  1. Sum the Elements of a Matrix Using a for Loop in MATLAB
  2. Sum the Elements of a Matrix Using the sum() Function in MATLAB
  3. Conclusion
How to Sum Elements of a Matrix in MATLAB

Summing the elements of a matrix is a fundamental operation in MATLAB, the powerful numerical computing environment. This tutorial will discuss the methods on how to sum the elements of a matrix in MATLAB.

Sum the Elements of a Matrix Using a for Loop in MATLAB

In MATLAB, matrices provide two types of indexing: row and column indexing and linear indexing.

Row and column indexing is where we specify the row and column numbers to access an element. On the other hand, linear indexing is where we access an element using its linear index.

Consider the following example:

m = [2 6 1; 17 19 18]
row_col_index = m(2,3)
linear_index = m(6)

Output:

m =

    2    6    1
   17   19   18

row_col_index = 18
linear_index = 18

In the code above, we demonstrate how to access the last element of the matrix using both types of indexing. With linear indexing, the elements are arranged in the matrix starting from the first column.

So, when we count from the first column, we find the last element at the sixth index. The advantage of linear indexing becomes apparent when iterating through a matrix; while row and column indexing would require two loops, with linear indexing, we only need a single loop.

In the following syntax, we use the variable total as an accumulator. The numel(matrix) function helps us find the total number of matrix elements.

Our for loop iterates through each element, accessed with matrix(i), and adds them to total to calculate the sum. This approach ensures efficient and concise matrix summation.

Syntax:

total = 0;
for i = 1:numel(matrix)
    total = total + matrix(i);
end

There are no explicit parameters in the syntax itself. However, the code assumes the use of a variable named matrix, which represents the input matrix whose elements you want to sum.

Additionally, there is a local variable total used to accumulate the sum, but it’s not a parameter; it’s a variable used within the code.

Let’s illustrate this by iterating through a matrix using linear indexing and calculating the sum of all the elements. See the code below.

m = [2 6 1; 17 19 18];
total = 0;
for i = 1:numel(m)
    total = total + m(i);
end
sumOfElements = total

Output:

sumOfElements = 63

The output is the total sum of the matrix’s elements, which is 63.

Sum the Elements of a Matrix Using the sum() Function in MATLAB

To find the sum of all the elements of a matrix, we can use the sum() function. In the case of a matrix, we have to use the sum() function two times, one for rows and one for columns, but in the case of a vector, we have to use the sum() only one time.

In the following syntax, we use matrix(:) to represent the matrix for summation. The (:) notation reshapes the matrix into a column vector, facilitating efficient summation of all elements.

Syntax:

totalSum = sum(matrix(:));

Parameters:

  • matrix(:): This parameter represents the matrix whose elements you want to sum.
  • (:): This notation reshapes the matrix into a column vector, making it suitable for summing all its elements efficiently.

The type of matrix should be a valid MATLAB matrix or multidimensional array.

For example, let’s find the sum of all the elements present in a given matrix. See the code below.

m = [2 6 1; 17 19 18];
sumOfElements = sum(sum(m))

In the code above, we initialize the matrix m with values. This matrix is a 2x3 matrix, meaning it has two rows and three columns, and the values inside the matrix are integers ([2 6 1; 17 19 18]).

Then, we use the sum() function twice here. The inner sum(m) calculates the sum of all elements in the matrix m.

It first sums the elements along the columns, resulting in a row vector where each element represents the sum of a column. Then, we apply the outer sum() function to this row vector to calculate the final sum, which is the sum of all the values in the matrix.

Lastly, the value of the calculated sum of all the elements is assigned to the variable sumOfElements, and it is displayed in the output.

Output:

sumOfElements = 63

The output is the total sum of the matrix’s elements, which is 63. We can also select the dimensions of the matrix on which you want to take the sum.

Conclusion

Summing matrix elements in MATLAB can be accomplished through various methods. Whether using a for loop to access elements individually or leveraging the concise power of the sum() function, MATLAB provides flexibility and efficiency for matrix summation.

By understanding these techniques, you can enhance your proficiency in numerical computation and data analysis, making MATLAB a valuable tool for a wide range of applications.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - MATLAB Matrix