I would suggest a short bsxfun solution:
%// number of divisions n = 4; %// unshuffled matrix like in your example unshuffled = bsxfun(@(a,b) mod(a,2*b) > b-1, meshgrid(1:n^2,1:n) - 1, (2.^((n-1):-1:0)).') %' %// shuffle columns shuffled = unshuffled(:,randperm(n^2))
unshuffled = 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 shuffled = 1 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 1 0 1 1 0 1 1 1 1 1 1 0 0 0 0 0 0 1 0 0 1 1 1
First you need to create an undeveloped matrix, which can be done by comparing the matrix generated using meshgrid(1:n^2,1:n) with a row-dependent module. Finally, you just need to shuffle the columns.
thewaywewalk
source share