Slice matrix upon "group ID" to get the mean

2 views (last 30 days)
Hello,
I have a vector containing data, and a second one containing only group ids so I know which data point is member of which group. Now I want to compute the mean of each group and subtract that mean from each datapoint. That should be done group wise of course and that's the problem. I can only think of a very brutal loop solution that is awful slow (already tried it). I made a small example code to help you better understand my problem:
x = [1,2,3,4,5,6,7,8,9,10]; % Data
y = [1,1,1,2,2,1,2,1,1,5]; % Vector containing the group id
g1 = [1,2,3,6,8,9]; % Vector containing only data of group 1
g2 = [4,5,7]; % Vector containing only data of group 2
g3 = [10]; % Vector containing only data of group 3
So I don't know how to get g1:g3 or in other words, I don't know how to tell Matlab that it should create a vector m and store the mean of each group in that vector. Afterwards Matlab should subtract the mean from the data point.
The solution should look like this:
m = [(29/6),(16/3),10]; % Vector with the mean of each group
x_demeaned = [1-m(1),2-m(1),3-m(1),4-m(2),5-m(2),6-m(1), . . .]; % demeaned data
Can you help me here? Thanks in advance!

Accepted Answer

Fangjun Jiang
Fangjun Jiang on 2 Jan 2012
x = [1,2,3,4,5,6,7,8,9,10]; % Data
y = [1,1,1,2,2,1,2,1,1,5]; % Vector containing the group id
[GroupId,indx_i,index_j]=unique(y);
GroupMean=arrayfun(@(k) mean(x(index_j==k)),1:length(GroupId))
New_x=x-GroupMean(index_j)
GroupMean =
4.8333 5.3333 10.0000
New_x =
Columns 1 through 7
-3.8333 -2.8333 -1.8333 -1.3333 -0.3333 1.1667 1.6667
Columns 8 through 10
3.1667 4.1667 0
  1 Comment
Léon
Léon on 3 Jan 2012
Thank you very much! I will learn that concept by heart. :-)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!