I use the following function in MATLAB to compute all binary vectors of length n in ascending order in log(2^n) steps.(All binary numbers of length n).
A = compV([0;1],n);
function [O] = compV(O,n)
j = length(O);
if n > 1
O1 = [zeros(j,1) O];
O2 = [ones(j,1) O];
O = [O1;O2];
O = compV(O,n-1);
end
end
I want to find an algorithm that computes recursively the same vectors but sorted by weight. And all that in relatively low complexity.
For example: 000 001 010 011 100 101 110 111 -> 000 001 010 100 011 101 110 111
I have run out of ideas and i am not sure if this can be done at all.
Thanks!
dec2bin(0:2^n-1)-'0'
? The output appears to be the same as your sample code. – kmac