I want to make an array called result
, which has dimensions (14,12,10)
and has values random[0 2]
, but it should not contain 0
more than three consecutive times in each row (dimension 2), and the sum of all values in each row must be <= 11
.
This is my current approach:
jum_kel = 14;
jum_bag = 12;
uk_pop = 10;
for ii = 1:uk_pop,
libur(:,:,ii) = randint(jum_kel,jum_bag,[0 2]); %#initialis
sum_libur(1,:,ii) = sum(libur(:,:,ii),2); %#sum each row
end
for jj = 1:jum_kel
while sum_libur(1,jj,ii) > 11, %# first constraint : sum each row should be <=11,
libur(jj,:,ii) = randint(1,jum_bag,[0 2])
sum_libur(1,:,ii)= sum(libur(:,:,ii),2);
for kk = 1:jum_bag
if kk>2
o = libur(jj,kk,ii)+libur(jj,kk-1,ii)+libur(jj,kk-2)
while kk>2 && o==0 %# constraint 2: to make matrix will not contain consecutive triplets (0-0-0) in any row.
libur(jj,:,ii) = randint(1,jum_bag,[0 2]);
sum_libur(1,:,ii)= sum(libur(:,:,ii),2);
end
end
end
end
end
but this is extremely slow...Does anyone see a faster method?