The mean function isn't working and i don't know why

조회 수: 10 (최근 30일)
Michael
Michael 2024년 10월 7일
댓글: Michael 2024년 10월 7일
I am trying to take the mean of a data set:
Data_IV_KE = Data(1:100,2);
xbar_IV_KE = mean(Data_IV_KE);
But I keep getting the error message:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
Error in Part_B_Exam (line 47)
xbar_IV_KE = mean(Data_IV_KE); % 4m/s
  댓글 수: 4
Walter Roberson
Walter Roberson 2024년 10월 7일
How did you read the csv file in?
I suspect you used readtable() into variable named Data so Data is a table() object, in which case Data(1:100,2) would be a table object not a numeric array.
Michael
Michael 2024년 10월 7일
I did use readtable and you were right about that being the problem. Thank you!

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

답변 (1개)

Torsten
Torsten 2024년 10월 7일
편집: Torsten 2024년 10월 7일
Check whether the data type of Data_IV_KE is "double".
To this end, MATLAB should return "double" when you type
class(Data_IV_KE)
as in
class(5)
ans = 'double'
I suspect it's not.
  댓글 수: 2
Walter Roberson
Walter Roberson 2024년 10월 7일
I suspect that Data is a table() object, so you would need
Data_IV_KE = Data{1:100,2};
Steven Lord
Steven Lord 2024년 10월 7일
Walter, mean on a table array works as of release R2023a, so if your suspicion that Data is a table array is correct the poster must be using a release of MATLAB that's a few years old.
A = gallery('moler', 4) % magic(4) would be boring for this calculation
A = 4×4
1 -1 -1 -1 -1 2 0 0 -1 0 3 1 -1 0 1 4
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean(A)
ans = 1×4
-0.5000 0.2500 0.7500 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
mean(array2table(A))
ans = 1x4 table
A1 A2 A3 A4 ____ ____ ____ __ -0.5 0.25 0.75 1
But I agree with you and Torsten, that Data is likely not a double array. Maybe a cell array?
C = num2cell(A)
C = 4x4 cell array
{[ 1]} {[-1]} {[-1]} {[-1]} {[-1]} {[ 2]} {[ 0]} {[ 0]} {[-1]} {[ 0]} {[ 3]} {[ 1]} {[-1]} {[ 0]} {[ 1]} {[ 4]}
mean(C)
Error using sum
Invalid data type. First argument must be numeric or logical.

Error in mean (line 122)
y = sum(x, dim, flag) ./ mysize(x,dim);

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by