Index exceeds the number of array elements (1).

SumA(1) = 0;
SumB(1) = 0;
for i = 2:10000
PlayerA = randi([1,2]);
PlayerB = randi([1,2]);
if (PlayerA + PlayerB) == 2
SumB = (SumB(i-1) + 1); %Line 21
elseif (PlayerA + PlayerB) == 3
SumA = (SumA(i-1) + 1); %Line 23
elseif (PlayerA + PlayerB) == 4
SumB = (SumB(i-1) + 1); %line 25
end
end
This is supposed to be a game of Odds vs Evens that is played 10,000 times.
The error messages change between lines 21, 23, 25. All have the same message. 'Index exceeds the number of array elements (1).'
I want the score (Variable 'Sum') to be a vector so I can plot any range from the vector (Points [1:50] or [1000: 1500] for example)
I don't understand what is causing the error to appear; is it a syntax error?

Answers (1)

SumA(1) = 0;
SumB(1) = 0;
for i = 2:10000
PlayerA = randi([1,2]);
PlayerB = randi([1,2]);
if (PlayerA + PlayerB) == 2
SumB(i) = (SumB(i-1) + 1); %
SumA(i) = SumA(i-1); %
elseif (PlayerA + PlayerB) == 3
SumA(i) = (SumA(i-1) + 1); %
SumB(i) = SumB(i-1); %
elseif (PlayerA + PlayerB) == 4
SumB(i) = (SumB(i-1) + 1); %
SumA(i) = SumA(i-1); %
end
end
Figured it out....

Asked:

on 19 Mar 2019

Answered:

on 19 Mar 2019

Community Treasure Hunt

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

Start Hunting!