How to calculate mean of a cell which contains numbers and empty string?
3 次查看(过去 30 天)
显示 更早的评论
I am working on cell in which there are 'NaN' values, along with the numbers. I need to calculate mean of 3x3 blocks after converting 'NaN' to empty string (''). I am doing this as I need mean of numbers only excluding 'NaN' entries, and '' are not counted while calculating mean. The error comes as 'mean()' can't be directly applied to cell, and I am unable to change the cell to matrix ('cell2mat'), as cell does not have all numbers but mix values.
See below for an example:
myCell ={ 1 2 3 4 5 NaN; 10 12 3 4 NaN NaN; NaN NaN NaN 1 4 5}
after replacing NaN by ''
myCell= { 1 2 3 4 5 ''; 10 12 3 4 '' ''; '' '' '' 1 4 5}
first 3x3 block is :
fisrtBlock = {1 2 3; 10 12 3; '' '' ''}
if I apply 'mean' after converting cell to matrix :
mean(cell2mat(firstBlock)),
the error is:
Error using cell2mat (line 46) All contents of the input cell array must be of the same data type.
if I try 'mean' directly: mean(firstBlock),
the error is:
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 28) y = sum(x)/size(x,dim);
if I try using 'cellfun':
f = cellfun(@mean, firstBlock),
the result is:
*f =
1 2 3
10 12 3
NaN NaN NaN*
I will appreciate any assistance for this problem. I am using MATLAB R2012a.
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!