The mean function isn't working and i don't know why
8 次查看(过去 30 天)
显示 更早的评论
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
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.
回答(1 个)
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)
I suspect it's not.
2 个评论
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
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
mean(A)
mean(array2table(A))
But I agree with you and Torsten, that Data is likely not a double array. Maybe a cell array?
C = num2cell(A)
mean(C)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!