Please help with for loop!!!

2 visualizaciones (últimos 30 días)
Jacob Savona
Jacob Savona el 17 de Feb. de 2015
Respondida: Star Strider el 17 de Feb. de 2015
I am trying to create a draft sort of thing. Where there are two teams team A and team B. They take turns choosing from 10 players(rand 3x10 matrix with first column as jersey number, second column as free throw %, and third row as rebounds/game). Team A wants best free throws. Team B wants best best rebounds. I have the loop to select these but I can't figure out how to delete the player after the team has selected him. Please help!!! Code:
A= zeros(3,10);
Z=[1:1:10];
A(1,:,:)=Z;
B=.5+rand(1,10)*.4;
A(2,:,:)=B;
C=round(1+rand(1,10)*7);
A(3,:,:)= C;
A
team_A= zeros(3,5);
team_B= zeros(3,5);
for d=[1:1:10]
j=10;
k=9;
if rem(d,2)==1;
for e=[1:1:j]
A(2,e,:)= max(B);
p=e;
team_A(:,2,:)=A(:,p,:);
A(:,p)=[];
end
elseif rem(d,2)==0
for f=[1:1:k]
A(3,f,:)= max(C);
i=f;
team_B(:,3,:)=A(:,i,:);
A(:,i)=[];
end
end
j=j-2;
k=k-2;
end

Respuesta aceptada

Star Strider
Star Strider el 17 de Feb. de 2015
Once I learned what your code was doing, this became a fun exercise!
This works. See if it does what you want:
A= zeros(3,10);
Z=[1:1:10];
A(1,:)=Z;
B=.5+rand(1,10)*.4;
A(2,:)=B;
C=round(1+rand(1,10)*7);
A(3,:)= C;
A
team_A= zeros(3,5);
team_B= zeros(3,5);
k1 = 0;
k2 = 0;
for d=[1:1:10]
if rem(d,2)==1;
k1 = k1 + 1;
p = Z(find(B(Z) == max(B(Z)),1,'first'))
team_A(:,k1)=A(:,p);
Z(Z==p)=[];
end
if rem(d,2) == 0
k2 = k2 + 1;
i = Z(find(C(Z) == max(C(Z)),1,'first'))
team_B(:,k2)=A(:,i);
Z(Z==i)=[];
end
end
I added a counter for each team, and removed the for loops inside the if blocks because they weren‘t necessary (they were actually causing problems). Keeping track of the draft picks turned out to be relatively straightforward. I used your ‘Z’ vector of indices into ‘A’, then each time a player was picked, deleted that player from ‘Z’. I added the ‘ 1,'first' ’ to find calls to avoid duplicate indices. I also deleted the third dimension from your matrices, because it wasn’t needed either. They’re all 2-dimensional as you set them up, so the third dimension was redundant.
I leave you to discover how the rest of my revision of your code works, since it’s all straightforward. (It’s GMT-7 here, and the end of my day.)

Más respuestas (0)

Categorías

Más información sobre Simulation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by