Need Help Simplifying code

1 次查看(过去 30 天)
Hi, I need help simplifying my final draft of an assignment. The assignment includes seperating 20 players to two teams: TeamA and TeamB. I have created a 3x20 matrix which includes player jerseys from 1-20, the second row is random free throw percentages from 50-90, and the third row is random average # of turnovers per game from 0-8. TeamA desires highest free throw percentage and TeamB desires lowest turnovers per game. A coin flip is done to decide which team starts the pick, then it is done ABABABAB... or BABABABA... Both teams will recieve 10 players each. I would like to get rid of the statements I also need help displaying only the final matrix of TeamA and final matrix of TeamB. Right now it is displaying each round at a time. Thanks!
numPlayers = 20;
ftPercent = randi([50,90],1,numPlayers);
avgNumTurnover = randi([0,8],1,numPlayers);
playerInfo = [1:1:numPlayers;ftPercent;avgNumTurnover];
TeamA = [];
TeamB = [];
% A 1 from the coinFlip is a heads
% A 0 from the coinFlip is a tails
PI=playerInfo;
for i = 1:numPlayers/2
coinFlip = round(rand(1,1));
if coinFlip == 1 % A first
pick=find(max(playerInfo(2,:))==playerInfo(2,:));
pick=pick(1);
TeamA=[TeamA playerInfo(:,pick)];
playerInfo(2,pick)=-999;
playerInfo(3,pick)=999;
pick=find(min(playerInfo(3,:))==playerInfo(3,:));
pick=pick(1);
TeamB=[TeamB playerInfo(:,pick)];
playerInfo(2,pick)=-999;
playerInfo(3,pick)=999;
else % B first
pick=find(max(playerInfo(2,:))==playerInfo(2,:));
pick=pick(1);
TeamA=[TeamA playerInfo(:,pick)];
playerInfo(2,pick)=-999;
playerInfo(3,pick)=999;
pick=find(min(playerInfo(3,:))==playerInfo(3,:));
pick=pick(1);
TeamB=[TeamB playerInfo(:,pick)];
playerInfo(2,pick)=-999;
playerInfo(3,pick)=999;
end % See teams each iteration
TeamA
TeamB
end
TeamA
TeamB
playerInfo=PI; % reset values

采纳的回答

Walter Roberson
Walter Roberson 2024-3-14
Replace
pick=find(max(playerInfo(2,:))==playerInfo(2,:));
pick=pick(1);
with
[~, pick] = max(playerInfo(2,:));
And you can also
playerInfo(2:3,pick)=-999;

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by