MATLAB: Moving players into teams – equal as possible random

equal random

hello all , i am a beginner at matlab , i have 33 players in my league , every player has an avg score ,
players_AVG=[166.29,156.57,155.72,171.29,156.76,155.67,168.36,161.77,155.65,173.92,162.06,149.21,177.44,161.79,148.64,177.25,162.15,146.02,175.28,162.72,142.59,174.21,164.88,141.91,180.69,168.75,127.06,184.18,165.39,133.26,191.41,185.23,90];
i want to insert the 33 players into 11 teams of 3 players each , i want the team_AVG to be as equal as possible . example output:
TEAM1=[player1(166.29),player5(156.57),player7(155.72)] –> TEAM1_AVG=159.53
TEAM2=[player2(171.29),player3(156.76),player4(155.67)] –> TEAM2_AVG=161.24
etc…
At the end of the year we want to reshuffle the teams.
i dont know where to start , how can i achieve this ??
thanks lampel.

Best Answer

Repeat several times: randperm(33), group by 3, use that to index averages, sum by team, std(), if lower than any found so far then remember the permutation