MATLAB: How to calculate the mean for the number of occurances

mean of occurances

suppose i have a cell C where
C{1,1}= ( 2 2 2 2 3 3 4 4 5);
C{2,1}=(3 4 4 4 5);
C{3,1}=(4 5);
and so on
i want the output as
x = (2 2 2 2 3 3 4 4 5);
the output should be the value which occurs the mean number of times of the occurances in the input. if the value is a decimal then it can rounded to a greater whole number for ex: the value 3 occurs twice in first cell and once in second cell. 2+1/2= 1.5 can be rounded to 2. so it occurs twice in the output.

Best Answer

u=unique([C{:}]);
n=cell2mat(cellfun(@(x) histc(x,u),C,'uni',0));
n(n==0)=nan;
x=cell2mat(arrayfun(@(x,n) repmat(x,1,n),u,round(nanmean(n)),'uni',0));
>> x =
2 2 2 2 3 3 4 4 5
>>