フィルターのクリア

How to calculate the sum of each row in a matrix?

407 ビュー (過去 30 日間)
afrya
afrya 2013 年 11 月 6 日
コメント済み: Jeevith 2023 年 10 月 16 日
Dear All,
I want to calculate the sum of each row in a large matrix.The matrix looks like:
y1 y2 y3 y4 y5
x2 2 4 4 6 7 Sumx2=??
x3 1 2 3 4 5 Sumx3=??
x4 1 2 3 4 5 etc
x5 1 2 4 5 6
My goal is to calculate for each x values the sum of the row. Do you have any idea to do that?
  1 件のコメント
Jeevith
Jeevith 2023 年 10 月 16 日
how to add a row

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 6 日
編集済み: Azzi Abdelmalek 2013 年 11 月 6 日
A=[2 4 4 6 7
1 2 3 4 5
1 2 3 4 5
1 2 4 5 6]
out=sum(A,2)
  2 件のコメント
afrya
afrya 2013 年 11 月 6 日
Ok thanks,so I have to extract the matrix A of my large matrix, because x and y are integer values.Do you know a way that enable to calculate directly the sum of each row, without extracting the matrix A?
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 6 日
編集済み: Azzi Abdelmalek 2013 年 11 月 6 日
for example, for row 1
s=sum(A(1,:))
For row n
n=3
s=sum(A(n,:))
You have to read about array
doc array

サインインしてコメントする。

その他の回答 (5 件)

Shubham Kumar
Shubham Kumar 2015 年 5 月 5 日
You can create a sum matrix over rows by typing sum(matrixName, 2). This will return an array containing sum over rows.
  2 件のコメント
James Tursa
James Tursa 2015 年 5 月 5 日
This is exactly the same answer that was already given and accepted 1 1/2 years ago.
Andrew Mccooey
Andrew Mccooey 2021 年 2 月 21 日
James Tursa. You're comment is extremely invaluable.

サインインしてコメントする。


Delladj Kamel
Delladj Kamel 2015 年 5 月 5 日
You can produce a sum vector over rows of matrix "A" by typing sum(A') where A' is the matrix transpose of matrix "A".

lurvik pokala
lurvik pokala 2021 年 8 月 8 日
how to calculate sum of all rows in a matrix given using matlab commands

loukil sana
loukil sana 2015 年 12 月 9 日
hi, i want to do some controle on the sum of each raw for example: y1 y2 y3 y4 y5 x2 2 4 4 6 7 Sumx2=1000 x3 1 2 3 4 5 Sumx3=1200 x4 1 2 3 4 5 Sumx4=1000 x5 1 2 4 5 6 Sumx5=1200
and i want to note that values given in the matrice are randomly genarating . thanks

Soumyabrata Bhattacharjee
Soumyabrata Bhattacharjee 2017 年 6 月 26 日
I don't know if someone has already highlighted this, I didn't go through all the answers. But coming to your question, you can try this
sum (A')'
where A is the input matrix
  2 件のコメント
Jan
Jan 2017 年 6 月 26 日
編集済み: Jan 2017 年 6 月 26 日
This works, but prefer sum(A, 2):
  • It saves the time for transposing
  • It calculates the wanted sum over the rows also if A is a coulmn matrix. Then the output must equal the input, but with sum(A')' a scalar is replied, because Matlab decides smartly to sum over the column.
Jaimie Ritchie
Jaimie Ritchie 2022 年 9 月 21 日
Thank you Jan! That is very helpful. I was able to use this to just find the sum of each row with columns 2 through 4 by using sum(A(:,2:4),2).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by