Help with this Matrix summation?

조회 수: 8 (최근 30일)
Jihun Sung
Jihun Sung 2017년 11월 2일
댓글: Star Strider 2017년 11월 2일
Hi guys,
I am having trouble with matrix summation.
For example, let's say Y = [5 6 7; 17 18 19]. If I do sum(Y), ans = [22 24 26]. Good, this is what I want.
But when Y = [17 18 19], the sum(Y) = 54.
How can I make it so that no matter what it takes the sum of, the answer will always output a row of sums of the columns of Y? I need this because for my function, I don't know what matrix values I will get, but I will always need an output that is a row vector.
Ex. Y = [17 18 19]. ans = [17 18 19].
Thank you!

채택된 답변

Star Strider
Star Strider 2017년 11월 2일
The second argument to sum is the dimension to sum across.
Example
Y = [5 6 7; 17 18 19];
col_sum = sum(Y,1)
row_sum = sum(Y,2)
col_sum =
22 24 26
row_sum =
18
54
  댓글 수: 2
Jihun Sung
Jihun Sung 2017년 11월 2일
Ah, so simple!
Thank you so much.
Star Strider
Star Strider 2017년 11월 2일
As always my pleasure!
The ‘dimension’ conventions can be a bit confusing until you get used to them.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by