JIYIK CN >

Current Location:Home > Learning > PROGRAM > MATLAB >

Sum of array elements in MATLAB

Author:JIYIK Last Updated:2025/04/18 Views:

This tutorial will discuss the use of function in Matlab sum()to find the sum of all the elements in an array.


Use the MATLAB sum()function to get the sum of array elements.

To get the sum of each element in an array, we can use the built-in function of Matlab sum(). If sum()the input to the function is a vector or an array containing numbers, then sum()the function will return an output which will be the sum of each element of the array or vector. For example, let's sum()find the sum of a vector using the function in Matlab. See the code below.

v = [1 2 6];
s = sum(v)

Output:

s =

     9

If the input to the sum function is a matrix, then the output of the sum function will be a row vector containing the sum of each column of the matrix. For example, if we have a three-column matrix, the output of the sum function will be a row containing three elements, and each element will be the sum of one column. For example, let's sum()find the sum of a matrix using the sum function in Matlab. See the code below.

v = [1 2 6; 8 6 5]
s = sum(v)

Output:

v =

     1     2     6
     8     6     5


s =

     9     8    11

As you can see, there are three columns in the matrix and the output vector also has three elements, corresponding to the sum of each column. If we do not want to sum each column, but want to sum each row; we can also define it as the second parameter in the sum function using the integer 2. For example, let's use sum()the function in Matlab to calculate the sum of the rows of a matrix. See the code below.

v = [1 2 6; 8 6 5]
s = sum(v,2)

Output:

v =

     1     2     6
     8     6     5


s =

     9
    19

As you can see, there are two rows in the matrix and the output vector also has two elements, corresponding to the sum of each row. We can also define the output data type as the second parameter in the sum function. For example, let us define the output data type as double. See the code below.

v = [1 2 6; 8 6 5]
s = sum(v,'double')

Output:

v =

     1     2     6
     8     6     5


s =

     9     8    11

We can also find the sum of all the elements of an array using loops in Matlab. For example, we can use a for loop, iterate through each element in an array, and add it to a variable. For example, let's start a variable at zero, and when the first element arrives, we add zero to it and save the result to the variable. When the second element comes, we add this to the current result, and we will keep doing this until we have iterated through all the elements of the array.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Commenting multiple lines in MATLAB

Publish Date:2025/04/18 Views:115 Category:MATLAB

This tutorial discusses how to comment multiple lines of code in MATLAB using the comment block method and the MATLAB Editor. Use comment blocks in MATLAB to comment multiple lines of code To comment one or two lines of code, we can % do it

Use mean() function in Matlab to get the average value of an array

Publish Date:2025/04/18 Views:145 Category:MATLAB

This tutorial will discuss finding the mean or average of an array using the function in MATLAB mean() . Use the MATLAB mean() function to find the average of an array To find the mean of an array, we can use Matlab's inbuilt function mean(

Find the index of a value in an array in Matlab

Publish Date:2025/04/18 Views:189 Category:MATLAB

This tutorial discusses find() finding the index of a value in an array using the function in MATLAB. Use the function in MATLAB find() to find the index of a value in an array In an array, elements are placed at certain indices starting fr

MATLAB sorts the rows

Publish Date:2025/04/18 Views:137 Category:MATLAB

This tutorial will discuss the use of the function in MATLAB sortrows() to sort the rows present in a matrix. In data analysis and processing, sorting is essential as it makes the data easy to analyze and process when it is sorted. For exam

如何在 Matplotlib Pyplot 中显示网格

Publish Date:2024/02/04 Views:145 Category:Python

本文演示了如何在 Python Matplotlib 中在一个图上画一个网格。使用 grid()函数来绘制网格,并解释了如何改变网格颜色和线条类型。

如何在 Matplotlib 中画一条任意线

Publish Date:2024/02/04 Views:168 Category:Python

本教程讲解了我们如何在 Matplotlib 中使用 matplotlib.pyplot.plot()、matplotlib.pyplot.vlines()、matplotlib.pyplot.hlines()方法和 matplotlib.collection.LineCollection 绘制任意线条。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial