Azzera filtri
Azzera filtri

splitting dataset to groups with equal means

5 visualizzazioni (ultimi 30 giorni)
Tom Salomon
Tom Salomon il 23 Ago 2016
Commentato: Robert il 24 Ago 2016
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

Risposte (1)

Robert il 23 Ago 2016
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
  2 Commenti
Tom Salomon
Tom Salomon il 23 Ago 2016
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 il 24 Ago 2016
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,[])

Accedi per commentare.


Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by