Finding the mean of subsets within cellarrays
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
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. :(
Regards!
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)));
end
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 29 Okt. 2012
[a,c,c] = unique(x(:,1));
avgx = [a,num2cell(accumarray(c,cell2mat(x(:,2)),[],@mean))];
0 Kommentare
Weitere Antworten (2)
Jing
am 29 Okt. 2012
Hi,
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)
avgx(i)=mean(cell2mat(x(strcmp(y{i},x(:,1)),2)));
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!