MATLAB: Index in position 1 exceeds array bounds (must not exceed 25). Please help!!

code debuggingMATLAB

function [Slotplayers,RoulettePlayers,HouseEarning]= hw5_func(SlotRounds,RouletteRounds)
Slotplayers=zeros(SlotRounds+1,10);
SlotPlayers(1, :)=round(5000+(25000-5000).*rand(1,10));
HouseEarning = 0;
for i=1:SlotRounds
Spin=rand(i,10);
for k=1:10
if SlotPlayers(i,k)>=100
if Spin(i,k)<= 0.00001
SlotPlayers(i+1,k) = SlotPlayers(i,k) + 250000;
elseif Spin(i,k)>0.00001 && Spin(i,k)<=0.00015
SlotPlayers(i+1,k)= SlotPlayers(i,k)+ 100000;
elseif Spin(i,k)>0.00015 && Spin(i,k)<=0.0005
SlotPlayers(i+1,k)= SlotPlayers(i,k) + 40000;
elseif Spin(i,k)>0.0005 && Spin(i,k)<=.005
SlotPlayers(i+1,k)= SlotPlayers(i,k) + 5000;
elseif Spin(i,k)>0.005 && Spin(i,k)<=0.07
SlotPlayers(i+1,k)= SlotPlayers(i,k) + 500;
elseif Spin(i,k)>0.07 && Spin(i,k)<=0.92434
SlotPlayers(i+1,k)= SlotPlayers(i,k) + 0;
end
end
end
HouseEarning(i+1)= HouseEarning(i)+(sum(SlotPlayers(i,:))-sum(SlotPlayers(i+1,:)));
end
This is the error I am getting:
Index in position 1 exceeds array bounds (must not exceed 67).
Error in hw5_func (line 35)
HouseEarning(i+1)= HouseEarning(i)+(sum(SlotPlayers(i,:))-sum(SlotPlayers(i+1,:)));
Thanks!

Best Answer

You initialize SlotPlayers with SlotRounds rows. When i becomes SlotRounds then you try to access SlotPlayers(i+1,:) which would be SlotPlayers(SlotRounds+1,:) which potentially does not exist because it was only initialized to SlotRounds.
In particular it will not be assigned to if SlotPlayers(SlotRounds,:) are all < 100.