Filter löschen
Filter löschen

Need Help Simplifying code

2 Ansichten (letzte 30 Tage)
Peter
Peter am 14 Mär. 2024
Kommentiert: Voss am 14 Mär. 2024
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 14 Mär. 2024
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;

Weitere Antworten (0)

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by