for iter=1:3 global ko; ko = 2; disp(sprintf('Iter %d: ', iter)) for i=1:4 perm=randperm(8); for j=1:ko s(perm(j),i)=1; end end s pause iter=iter+1;end
I'm trying to create a matrix of size [8×4] and the value for each is randomly assign 1 and must not exceed the variable ko. However, for every iteration, the number of 1 assign is exceeding the value of ko. Is there any problem to my code?
Sample answer:
Iter 1:
s =
0 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0
Iter 2:
s =
1 1 1 1 0 0 1 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 0 0 0 1 0 1 0 0
Iter 3:
s =
1 1 1 1 0 0 1 0 0 0 0 1 1 1 1 0 1 0 1 1 1 1 0 1 0 0 1 1 0 1 1 1
Best Answer