Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help to Write code to Create Binary Vectors

Subject: Help to Write code to Create Binary Vectors

From: Isaac

Date: 20 Oct, 2011 17:01:27

Message: 1 of 4

Hi All!

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".

For example

lenght=3
number of ones=2

my function should return me the following matrix (one row = one combination):

[1 1 0;
1 0 1;
0 1 1]


could you suggest me any efficient way to do it?

Thanks in advance

Asi

Subject: Help to Write code to Create Binary Vectors

From: Roger Stafford

Date: 20 Oct, 2011 20:11:10

Message: 2 of 4

"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

Subject: Help to Write code to Create Binary Vectors

From: Roger Stafford

Date: 21 Oct, 2011 00:04:09

Message: 3 of 4

"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

Subject: Help to Write code to Create Binary Vectors

From: Isaac

Date: 21 Oct, 2011 09:08:09

Message: 4 of 4

"Roger Stafford" wrote in message <j7qctp$jgp$1@newscl01ah.mathworks.com>...
> "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

thanks a lot Roger!
You have been really kind!

Asi

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us