MATLAB Answers


Finding the mean of subsets within cellarrays

Asked by Léon
on 28 Oct 2012


I have a panel dataset as a cellarray and one cell column represents the index of a data subset within this array (long format). Now I want to create a vector with the mean of each subset. I tried the following but this does not work completely and I would be more than happy if you could help me out here. I did that several times in STATA, but there you can handle panel data directly and specify an index variable in your dataset, obviously you have to do the work in Matlab completely by yourself. :(


x = {'A',2.5;'A',5.0;'B',2.6}
y = unique(x(:,1))
mean = zeros(length(y),1)
for i = 1:length(y)
   mean(i) =  mean(x(x{:,2}==y(i)));




No products are associated with this question.

3 Answers

Answer by Andrei Bobrov
on 29 Oct 2012
 Accepted answer

[a,c,c] = unique(x(:,1));
avgx = [a,num2cell(accumarray(c,cell2mat(x(:,2)),[],@mean))];


Answer by Jing
on 29 Oct 2012


In your code, you can't index into a cell array using ':', and if you want to use the build-in MEAN function, you should not define a variable as 'mean', because MATLAB will treat it as a variable first. I think the following code can complete your goal.

x = {'A',2.5;'A',5.0;'B',2.6};
y = unique(x(:,1))
avgx= zeros(length(y),1);
for i = 1:length(y)


Answer by Léon
on 29 Oct 2012
Edited by Léon
on 29 Oct 2012

This is exactly what I've been searching for and the speed of that is amazing. Thank you very much! :)

May I ask a question that builds on top of that?

Is it possible to apply the function on a sub-sub-set as well? Considering we have an additional date vector:

day = (28;29;30);

can we compute the mean within the subgroup, but day wise?

  1 Comment

on 30 Oct 2012

What do you mean by day-wise?

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!