splitting dataset to groups with equal means

조회 수: 1 (최근 30일)
Tom Salomon
Tom Salomon 2016년 8월 23일
댓글: Robert 2016년 8월 24일
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에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by