samedi 7 mars 2015

Pick a number randomly from a vector in Octave


St=[-1 5 -1 2 ; -1 6 1 3 ; -1 7 2 4; -1 8 3 -1 ; 1 9 -1 6 ; 2 10 5 7 ; 3 11 6 8 ; 4 12 7 -1; 5 -1 -1 10 ; 6 -1 9 11 ; 7 -1 10 12 ; 8 -1 11 -1];
countlegal=0;
a=2;
X=St(2,:); % pull out the entire row of R to find out other actions
%count the other legal moves apart from a

for i=1:4,
if X(i)==-1 || i==a %dont count if a or illegal move
continue;
else
countlegal=countlegal+1;

Ac(countlegal)=i;
Ac
end
end

if size(Ac,2)==1
a1=Ac
else
a1=datasample(Ac,1);%comment 1: this command does not work , here I want to pick an element randomly from Ac
end
a1



Ac is a row vector which is dynamically assigned values. I want to pick an element from Ac and assign it to a1 . The datasample command(comment1) does not work and randperm does not work since it generates numbers from 1 to n . Your help is much appreciated.






Aucun commentaire:

Enregistrer un commentaire