필터 지우기
필터 지우기

Find the average of each row, ignore the first column

조회 수: 14 (최근 30일)
Eric Brown
Eric Brown 2022년 4월 30일
댓글: Voss 2022년 4월 30일
This is pretty straight foward, probaly something small I'm missing but as the title says, mean of each row starting at element 2. I'm getting a 3 by 1 array when i should be getting a 4 by 1 sized array. So whats wrong with my code?
load C.dat
A = C(:,2:end);
Cave= mean(A)';
  댓글 수: 2
Riccardo Scorretti
Riccardo Scorretti 2022년 4월 30일
Which is the size of C?
Eric Brown
Eric Brown 2022년 4월 30일
C is a 2 dimensional array

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

채택된 답변

Voss
Voss 2022년 4월 30일
Let me make up a matrix C
C = (1:4)+[1;2;3;4]*10
C = 4×4
11 12 13 14 21 22 23 24 31 32 33 34 41 42 43 44
By default, mean operates along the first dimension, so this
A = C(:,2:end);
Cave= mean(A)'
Cave = 3×1
27 28 29
is taking the mean of each column of A, i.e., the mean of columns 2 through 4 of C. I imagine you intended to do this
A = C(:,2:end);
Cave= mean(A')'
Cave = 4×1
13 23 33 43
But a more direct way is to specify the dimension along which mean should operate. In this case, dimension 2 to operate along the rows
Cave = mean(C(:,2:end),2)
Cave = 4×1
13 23 33 43
  댓글 수: 2
Eric Brown
Eric Brown 2022년 4월 30일
편집: Eric Brown 2022년 4월 30일
I tried doing it the way you did in your last example but couldn't figure out the syntax of it so i gave up on that approach and did it the less effient way. Thank you for your help and explanation.
Voss
Voss 2022년 4월 30일
You're welcome!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by