필터 지우기
필터 지우기

Don't know how to fix... Index in position 1 exceeds array bounds (must not exceed 1).

조회 수: 2 (최근 30일)
Hello--
I keep getting this error and i've tried to fix it for so long. it is occuring on lines between the lines specified bellow (I bolded them). If anyone can figure out why I would appreciate it. I know it is an indexing error, but I can't figure out where I messed up.
for l = 1:RouletteRounds
x = (randi([2 12],1,10)) *.01; %random number between 2-12%
Bet = x .* RoulettePlayers(l,:); %creates a vector Bet that is 2-12% of in pocket cash
for ll = 1:RouletteRounds
if RoulettePlayers(1,ll) > 100 %if cash is greater than 100
if Bet(1,ll) < 100 %enters if statement: if bet is less than 100, = 100
Bet(1,ll) = 100;
else
end
else
Bet(1,ll) = 0;
end
end %2nd for loop
BetType(1,:) = randi([-4,-1],1,10);
for r = 1:RouletteRounds %for loop creating different bets and the intervals
if BetType(1,r) == -3
BetType(2,r) = randi([0,36])
elseif BetType(1,r) == -4
BetType(2,r) = randi([0,36])
BetType(3,r) = randi([0,36])
end
end
Roll = randi([0,36],1,10)
%9 compare the actual roll and seeing if they made money
ERROR IN THIS FORLOOP ON MULTIPLE LINES I GET ERROR Index in position 1 exceeds array bounds (must not exceed 1). FORLOOP BELOW ->>>
for t = 1:RouletteRounds
if BetType(1,t) == -3
if Roll(1,t) == BetType(2,t) %if bet is equal to bettype column 2
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) + Bet(l,t)*36
else
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) - Bet(l,t)
end
elseif BetType(1,t) == -4
if Roll(1,t) == BetType(2,t) || Roll(1,t) == BetType(3,t) %if bet is equal to bettype column 2,3
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) + Bet(l,t)*18
else
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) - Bet(l,t)
end
elseif BetType(1,t) == -1
if Roll(1,t) == 1:18
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) + Bet(l,t)
else
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) - Bet(l,t)
end
elseif BetType(1,t) == -2
if Roll(1,t) == 19:36
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) + Bet(l,t)
else
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) - Bet(l,t)
end
end
end
HouseEarnings = HouseEarnings + (sum(RoulettePlayers(1,:)) - sum(RoulettePlayers(end,:)))
end %(1st for loop)
  댓글 수: 2
KSSV
KSSV 2019년 2월 27일
편집: KSSV 2019년 2월 27일
We cannot help it without having data or all variables defined in hand. On top of it, you have not mentioned exact line where error occurs.
Brian Peoples
Brian Peoples 2019년 2월 28일
%Roulette
RouletteRounds = 10;
RoulettePlayers = zeros(RouletteRounds+1,10);
initial_intt = randi([5000,25000],1,10);
RoulettePlayers(1,:) = initial_intt
Bet = zeros(1,10);
BetType = zeros(3,10);
So sorry about this!! Ignore the HouseEarnings variable at the end of the loop I sent, there is no problem with that. The error in the code occurs in the loop I specified (getting errors on this part of the for loop above:
elseif BetType(1,t) == -4
if Roll(1,t) == BetType(2,t) || Roll(1,t) == BetType(3,t) %if bet is equal to bettype column 2,3
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) + Bet(l,t)*18
else
RoulettePlayers(l+1,t) = RoulettePlayers(l,t) - Bet(l,t)
end

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2019년 2월 28일
The problem is the ‘Bet’ subscript. It is a (1 x 10) vector, not a matrix, so it should have only one subscript, not two.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by