In a single array it's pretty simple to get the mean
or standard deviation (std
) of its numbers, however in a cell, whose data doesn't have the same size in each of its positions I couldn't do mean2
or std2
.
I know it's possible to do something if I copy all of the data in a single row or single column but I just wanted to ask if someone knows if there is a single formula to do it?
Thanks
You can use cellfun
to compute per-cell mean
and std
:
cell_mean = cellfun(@mean, my_cell);
cell_std = cellfun(@std, my_cell);
For example:
>> my_cell = {[1,2,3,6,8], [2,4,20]}
>> cellfun(@mean, my_cell)
ans =
4.0000 8.6667
>> cellfun(@std, my_cell)
ans =
2.9155 9.8658
If you want the mean
and/or std
of all the elements in all the cells, you can:
>> mean([my_cell{:}])
ans =
5.7500
>> std([my_cell{:}])
ans =
6.2048
And, if your cell elements are all of different sizes, you can use cell2mat
to assist you:
>> mean(cell2mat(cellfun(@(x) x(:)', my_cell, 'uni', 0)))
ans =
5.7500
>> std(cell2mat(cellfun(@(x) x(:)', my_cell, 'uni', 0)))
ans =
6.2048