"Array exceed the predefined size"

2 次查看(过去 30 天)
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 个评论
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