フィルターのクリア

Need Help Simplifying code

2 ビュー (過去 30 日間)
Peter
Peter 2024 年 3 月 14 日
コメント済み: Voss 2024 年 3 月 14 日
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;
  1 件のコメント
Voss
Voss 2024 年 3 月 14 日
playerInfo(2:3,pick)=[-999;999];

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by