"Array exceed the predefined size"

2 visualizaciones (últimos 30 días)
Liming Fang
Liming Fang el 2 de Mayo de 2020
Comentada: Liming Fang el 2 de Mayo de 2020
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 comentarios
darova
darova el 2 de Mayo de 2020
PLease show the whole code
Liming Fang
Liming Fang el 2 de Mayo de 2020
Editada: Liming Fang el 2 de Mayo de 2020
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;

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 2 de Mayo de 2020
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 comentarios
Walter Roberson
Walter Roberson el 2 de Mayo de 2020
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 el 2 de Mayo de 2020
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

Iniciar sesión para comentar.

Categorías

Más información sobre Performance and Memory en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by