Invalid data type. First argument must be numeric or logical.

27 次查看(过去 30 天)
[RT_CD_row,RT_CD_col]=size(RT_CD_Data);
for C=1:1:RT_CD_row
RT=(RT_CD_Data{C,1});
AvgRT(C,1)=mean([RT(:,3)]); //This is third column in RT_CD_data of 1st column
end
I'm trying to take average of cell array of each column. RT_CD_Data is reading data which is 12x1 cell array from excel file. I got an error says
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 Project (line 27)
AvgRT(C,1)=mean([RT(:,3)]);
How Can I get average of those cell arrays. like average of RT_CD_Data{C,1}(:,3)?
  4 个评论
Guillaume
Guillaume 2019-11-8
Your cell array contains cell arrays, not matrices. We don't know what's in these subcell arrays. It's easier for us to find out if you attach your data in a mat file instead of posting screenshots.
There's a lot of strange syntax in your code. This will puzzle the reader and make them wonder what it is you're trying to do.
E.g. what are you trying to do with the () in:
RT=(RT_CD_Data{C,1});
the () don't do anything. Similarly, what are you trying to do with the [] in
.. = mean([RT(:,3)])
again, the brackets don't do anything.
Also note that none of the excel import functions create cell arrays, so you must have done some additional processing to your imported data. It's likely that there is a better way of manipulating this data, one that doesn't produce a cell array of cell arrays (of something we don't know).
Adam Danz
Adam Danz 2019-11-8
编辑:Adam Danz 2019-11-8
@Ashlesh Patel , think about this from an outsider's perspective.
  1. The image you shared is a little helpful. It tells use that you've got some variable that appears to be a cell array with at least 12 rows and at least 2 columns. The first column contains empties. The 2nd colum contains cell arrays that each have 3 columns and 108-123 rows but we don't know each element of those cell arrays are. Are they a 3rd layer of cell arrays? Are they matrices? Which variable are you showing?
  2. If the data you shared is RT_CD_Data, then that's even more confusing because your function is operating over the 1st column which contains all empties.

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2019-11-8
编辑:Adam Danz 2019-11-8
My guess is that....
[RT_CD_row,RT_CD_col]=size(RT_CD_Data);
for C=1:1:RT_CD_row
RT=(RT_CD_Data{C,1});
% ^ This needs to be 2 (not 1)
AvgRT(C,1)=mean([RT(:,3)]); //This is third column in RT_CD_data of 1st column
end
But that can be cleaned up a bit....
% re-create similarly structured data
RT_CD_Data = arrayfun(@(x)num2cell(rand(x,3)), randi(10,12,1)+107,'UniformOutput', false);
RT_CD_Data = [cell(size(RT_CD_Data)), RT_CD_Data];
% Loop method
AvgRT = nan(size(RT_CD_Data(:,2))); % <-- don't forget to pre-allocate the loop array
[RT_CD_row,RT_CD_col]=size(RT_CD_Data);
for C=1:1:RT_CD_row
RT=(RT_CD_Data{C,2});
% ^ 2nd column of cell array
AvgRT(C,1)=mean([RT{:,3}]);
% ^ 3rd column of the matrix
end
And the block above can be done in 1 line of code
% No loop method
AvgRT = cellfun(@(x)mean([x{:,3}]), RT_CD_Data(:,2));
% 3rd col of matrix ^ ^ 2nd col of cell
*Updated based on Guillaume's catch of my error
  5 个评论
Adam Danz
Adam Danz 2019-11-8
编辑:Adam Danz 2019-11-8
@Ashlesh Patel, you can always update to add more information or correct an error but you can't delete the question.
If this answer doesn't address your problem, please explain why. If the demo data do no have the same structure as your real data, describe the difference. If you're getting an undesired output, describe that, too.
Adam Danz
Adam Danz 2019-11-8
编辑:Adam Danz 2019-11-9
@Ashlesh Patel, that alone will not solve the problem. The problem is that you are operating on the 1st column of your data which are all empties. 'C' controls which rows your referencing. Maybe you need to change the loop indices (C ) too (we couldn't possibly know that since that isn't mentioned in your question).

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by