How to calculate the sum of each row in a matrix?
이전 댓글 표시
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?
채택된 답변
추가 답변 (5개)
Shubham Kumar
2015년 5월 5일
6 개 추천
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
2015년 5월 5일
This is exactly the same answer that was already given and accepted 1 1/2 years ago.
Andrew Mccooey
2021년 2월 21일
James Tursa. You're comment is extremely invaluable.
Delladj Kamel
2015년 5월 5일
3 개 추천
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
2021년 8월 8일
1 개 추천
how to calculate sum of all rows in a matrix given using matlab commands
loukil sana
2015년 12월 9일
0 개 추천
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
2017년 6월 26일
0 개 추천
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
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
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).
카테고리
도움말 센터 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!