splitting dataset to groups with equal means

1 次查看(过去 30 天)
I have a data vector which I would like to divide into n equal size groups. Now the trick is, I would like these groups to have an identical mean, or at least as similar as possible. e.g. for n=2, X=1:8 could be divided into this result X_sol=[1,2;4,3;6,5;7,8] or X_sol=[1,2;4,3;5,6;8,7], etc. where each column of X_sol has a mean of 4.5
Thanks!

回答(1 个)

Robert
Robert 2016-8-23
If your variables are sized similarly to your example, you can take the brute force approach and use perms to test every possible arrangement.
n = 3;
x = randn(9,1)+1;
% make a list of every arrangement of x into n groups
y = reshape(perms(x),[],length(x)/n,n);
% find the combo with the least sum squared of the difference between the means
z = mean(y,2);
% take the difference with the first mean
z = bsxfun(@minus,z(:,:,2:end),z(:,1,1));
% find the one with the least sum of the squares
[z,ii]=min(sum(z.^2,3))
x=squeeze(y(ii,:,:))
mean(x)
  2 个评论
Tom Salomon
Tom Salomon 2016-8-23
That's a nice solution, but unfortunately not suitable for my needs. I do need a more refined solution.
My vector length is about 60-80, so there are way too many possible combinations.
for x with length(x)=80, n=2, that's 1.07507E+23 possible combinations.
MATLAB can't handle these variable sizes.
Robert
Robert 2016-8-24
Your problem is a version of the Partition Problem. There are lots of clever but complex ways to approach this problem. The specifics of your data and your needs will determine which solution is best for you.
You might try the simplest first in case it is enough. Simply sort the data, then move them into groups in order.
n = 3;
data = randi(100,[90,1]);
grouped = reshape(sort(data),n,[])
mean(grouped,2)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by