"Array exceed the predefined size"

조회 수: 2 (최근 30일)
Liming Fang
Liming Fang 2020년 5월 2일
댓글: Liming Fang 2020년 5월 2일
I'm running a simple program, and sometimes(yeah) i would be stuck in the following error:
What's more, it said that i wanted to create an array with size of 1x18446744073709551615 (17179869184.0GB)
OMG....
I'm just concating two arrays...
"player = [player,deck(1)]; deck = deck(2:end); "
From the environment section, i saw that the array was all fine, but when i click the botton to step, the program abort.
the player and deck are just two small arrays.
HELP.
  댓글 수: 3
darova
darova 2020년 5월 2일
PLease show the whole code
Liming Fang
Liming Fang 2020년 5월 2일
편집: Liming Fang 2020년 5월 2일
Here is the main part of the code which i think is problematic
It's just an simulation of blackjack
clc;clear;
N_EPISODES = 4e5;
N_PLAYER_VALUE = 21-12+1;
N_DEALER_VALUE = 13;
N_ACE = 2;
N_ACTION = 2;
tic
for i=1:N_EPISODES
deck = shufflecards();
stateseen = [];
player = deck(1:2); deck = deck(3:end);
dealer = deck(1:2); deck = deck(3:end);
dealer_showed = dealer(1);
phv = handValue(player);
dhv = handValue(dealer);
while(phv<12)
player = [player,deck(1)]; deck = deck(2:end);
phv = handValue(player);
end
si = 1;
action = unidrnd(2)-1;
state = stateFromHand(player,dealer_showed);
P(sub2ind([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE],state(1,1)-11,state(1,2),state(1,3)+1)) = action;
stateseen(si,:) = state;
while((phv<=21)&&(action))
player = [player,deck(1)]; deck = deck(2:end); phv = handValue(player);
state = stateFromHand(player,dealer_showed);
stateseen(end+1,:) = state;
if(phv<=21)
si = si+1;
action = P(sub2ind([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE],state(1)-11,state(2),state(3)+1));
end
end
% dealer's turn
while(dhv<17)
dealer = [dealer,deck(1)]; deck = deck(2:end);
dhv = handValue(dealer);
end
end
toc
And the error occurs in line22
"player = [player,deck(1)]; deck = deck(2:end);"
Oh, the shufflecards equals "randperm(52)"
function [hv,usableAce] = handValue(hand)
% it calculates the all of the cards number in my hand
% handValue([10 11]) ->20
% compute 1:13 indexing for each card:
values = mod( hand - 1, 13 ) + 1;
% map face cards (11,12,13)'s to 10's:
values = min( values, 10 );
sv = sum(values);
% Promote soft ace
if (any(values==1)) && (sv<=11)
sv = sv + 10;
usableAce = 1;
else
usableAce = 0;
end
hv = sv;
function [st] = stateFromHand(hand,cardShowing)
%
% Returns the state (a three vector of numbers for a given hand of cards)
%
% [players current sum, dealar showing card, usable ace]
%
% Cards are enoumerated 1:52, such that
%
% 1:13 => A, 2, 3, ..., 10, J, Q, K (of C)
% 14:26 (of D)
% (of H)
% (of S)
[hv,usableAce] = handValue(hand);
cardShowing = mod( cardShowing - 1, 13 ) + 1;
st = [ hv, cardShowing, usableAce ];
return;

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

답변 (1개)

Walter Roberson
Walter Roberson 2020년 5월 2일
I am not seeing that error.
I had to guess what your shufflecards function does.
I encounter
Index exceeds the number of array elements (14).
Error in jackall (line 39)
action = P(sub2ind([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE],state(1)-11,state(2),state(3)+1));
Which makes sense. You do not initialize P, and you store into
P(sub2ind([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE],state(1,1)-11,state(1,2),state(1,3)+1)) = action;
which builds it out to a certain size. Then you
state = stateFromHand(player,dealer_showed);
which changes state, and you try to do
action = P(sub2ind([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE],state(1)-11,state(2),state(3)+1));
but since the first element of the state increased due to the additional card being taken into account, the right hand side is trying to index into P at a location beyond what you initialized.
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 5월 2일
I did guess that shufflecards was randperm(52)
>> type shufflecards.m
function shuffled = shufflecards()
shuffled = randperm(52);
end
I used your exact code other than guessing (correctly) for shufflecards, and I encountered the error message I indicated above.
Liming Fang
Liming Fang 2020년 5월 2일
emm,sorry, indeed, i initialized "P". I just forgot to upload it.
Q = zeros(prod([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE]),N_ACTION);
P = ones(prod([N_PLAYER_VALUE,N_DEALER_VALUE,N_ACE]),1);
These two sentences are at the top of the .m file

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

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by