Need Help Simplifying code

조회 수: 1 (최근 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 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