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!
Best Answer