|
"Roger Stafford" wrote in message <j7pv8u$7c3$1@newscl01ah.mathworks.com>...
> "Isaac " <asimov4000@hotmail.com> wrote in message <j7pk57$qbq$1@newscl01ah.mathworks.com>...
> > I have to write a code which generates all the possibile binary vectors (i.e. vectors whose elements can be only 0 or 1) which a determined length and "numbers of ones".
> > ........
> - - - - - - - - - - -
> Use 'nchoosek' with its first argument as 1:length and the second argument as the desired number of ones. Each row of output will be indices of the positions of ones in a possible binary vector.
>
> Roger Stafford
- - - - - - - -
Perhaps I should have given an answer in actual matlab code. Let n be the total length of the binary vectors and k the desired number of ones in each.
C = nchoosek(0:n-1,k);
m = size(C,1);
B = zeros(m,n);
B(bsxfun(@plus,(1:m).',m*C)) = 1;
Then B is an m by n array consisting of all possible binary vector rows with k ones.
Roger Stafford
|