Reputation: 679
I want to make a code that can calculate 10 percentiles out of a matrix consisting of two values for each row: the numer of worked hours and the wage. The percentiles shall be based on the wage, but show the average worked hours for the individuals within the wage span.
In total I have about 3000 rows in my matrix, but here is how the data looks like (the real data has no headlines, I just put them in here to make it more readable):
Wage/h Work(h)
12 1572
15 1671
32 1782
78 1254
22 0
I understand that prctile
must be the right function to use, but I do not know how to use it. Thankful for all help. :-)
Upvotes: 0
Views: 1088
Reputation: 65430
You want to use prctile
to compute the percentiles of your data. You can then use bsxfun
and >=
to compare each data point to each of the percentile values. You can then use cumsum
to provide a group index for each data point and then use accumarray
to compute the mean for each group.
% Compute the percentiles
percentiles = prctile(data(:,1), 0:10:90);
% Determine which percentile each wage is within
tmp = cumsum(bsxfun(@ge, data(:,1), percentiles), 2);
group = tmp(:,end);
% Compute the mean hours for each group
mean_wages = accumarray(group, data(:,2), [], @mean);
Upvotes: 2