How to calculate the mean of each row, excluding the first column?

조회 수: 4 (최근 30일)
Julia
Julia 2023년 2월 12일
댓글: Julia 2023년 2월 12일
For example, if I have this matrix
M=[0 3 4
0 6 5
0 9 2
0 3 1
0 8 9
0 4 6]
How can I calculate the mean of each row, excluding that 0? I tried
A=mean(M(:,2:end))
but this code gives me the mean of each column instead of each row.

채택된 답변

Arif Hoq
Arif Hoq 2023년 2월 12일
M=[0 3 4
0 6 5
0 9 2
0 3 1
0 8 9
0 4 6];
A=mean(M(:,2:end),2)
A = 6×1
3.5000 5.5000 5.5000 2.0000 8.5000 5.0000
  댓글 수: 2
Askic V
Askic V 2023년 2월 12일
It is also possible to use transpose:
A=mean(M(:,2:end).').'

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by