Slice matrix upon "group ID" to get the mean
Show older comments
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!
1 Comment
Walter Roberson
on 2 Jan 2012
There's probably a good use for accumarray here.
Accepted Answer
More Answers (1)
For those stumbling on this more recently, MATLAB now has the grouptransform function introduced in R2018b. It can be used to make grouped calculations where the result is the same size as the input. It even has a built-in method for subtracting off group means:
x = [1,2,3,4,5,6,7,8,9,10]';
y = [1,1,1,2,2,1,2,1,1,5]';
grouptransform(x,y,"meancenter")
Categories
Find more on Exploration and Visualization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!