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:
how to generate all vectors of length n with entries are 0's or 1's then sum over

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: yang

Date: 13 Jul, 2010 18:15:21

Message: 1 of 14

Hi,
I need your help for this problem, please.

\mathop {\sum {...\sum }}\limits_{z_r \in {Q_{2}^{*}}} such that

Q_{2}^{*}=\{(z_1,...,z_r):z_{j}=0,1,\sum_{j=1}^{r}z_{j}=\ell\}, where \ell = 0, 1, . . . , r.

For example. If r=3, \ell=1; then we are looking for triples with elements 0's or 1's and sum to 1 i.e. Q_{2}^{*}=\{(0,0,1),(0,1,0),(1,0,0)\}. So we would like to sum over theses z_r's.

I appreciate your help.
Adam

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: Roger Stafford

Date: 13 Jul, 2010 19:05:21

Message: 2 of 14

"yang " <macmaster_egypt@yahoogroups.com> wrote in message <i1iafp$6v7$1@fred.mathworks.com>...
> Hi,
> I need your help for this problem, please.
>
> \mathop {\sum {...\sum }}\limits_{z_r \in {Q_{2}^{*}}} such that
>
> Q_{2}^{*}=\{(z_1,...,z_r):z_{j}=0,1,\sum_{j=1}^{r}z_{j}=\ell\}, where \ell = 0, 1, . . . , r.
>
> For example. If r=3, \ell=1; then we are looking for triples with elements 0's or 1's and sum to 1 i.e. Q_{2}^{*}=\{(0,0,1),(0,1,0),(1,0,0)\}. So we would like to sum over theses z_r's.
>
> I appreciate your help.
> Adam
- - - - - - - -
  If I interpret your question correctly, for r = 5 and ell = 3, you first want all sequences of five 0's or 1's whose sum is 3:

 0 0 1 1 1
 0 1 0 1 1
 0 1 1 0 1
 0 1 1 1 0
 1 0 0 1 1
 1 0 1 0 1
 1 0 1 1 0
 1 1 0 0 1
 1 1 0 1 0
 1 1 1 0 0

Next you want the sum of each of these columns. Is that right?

  You don't need matlab for that trivial problem. The answer is always

 (r-1)!/(ell-1)!/(r-ell)!

for each column. In the above case this is 4!/2!/2! = 6 which is clearly correct.

  Or, if you want the sum over all elements of all sequences, it would be

 r!/(ell-1)!/(r-ell)!

Roger Stafford

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: yang

Date: 13 Jul, 2010 19:32:05

Message: 3 of 14

Thanks for your quick reply,

1-let us called them
 
> z1=( 0 0 1 1 1)
> z2=(0 1 0 1 1)
> z3=(0 1 1 0 1)
> z4=(0 1 1 1 0)
> z5=(1 0 0 1 1)
> z6=(1 0 1 0 1)
> z7=(1 0 1 1 0)
> z8=(1 1 0 0 1)
> z9=( 1 1 0 1 0)
> z10=(1 1 1 0 0)
>
How can i generate them?
then I would like to sum over z1, z2,....,z10 for something like (zi/3)+(1-zi)/8.

Thanks.

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: Roger Stafford

Date: 13 Jul, 2010 19:33:06

Message: 4 of 14

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message >
> You don't need matlab for that trivial problem. The answer is always
> .......
> (r-1)!/(ell-1)!/(r-ell)!
>
> for each column. In the above case this is 4!/2!/2! = 6 which is clearly correct.
>
> Or, if you want the sum over all elements of all sequences, it would be
>
> r!/(ell-1)!/(r-ell)!
> .......
- - - - - - -
  I see that both those formulas fail for ell = 0. You can rewrite them as

 (r-1)!*ell/ell!/(r-ell)!

and

 r!*ell/ell!/(r-ell)!

respectively, to cover that case.

Roger Stafford

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: yang

Date: 13 Jul, 2010 19:38:04

Message: 5 of 14

Thanks for your quick reply,

1-let us called them
 
> z1=( 0 0 1 1 1)
> z2=(0 1 0 1 1)
> z3=(0 1 1 0 1)
> z4=(0 1 1 1 0)
> z5=(1 0 0 1 1)
> z6=(1 0 1 0 1)
> z7=(1 0 1 1 0)
> z8=(1 1 0 0 1)
> z9=( 1 1 0 1 0)
> z10=(1 1 1 0 0)
>
How can i generate them?
then I would like to sum over z1, z2,....,z10 for something like (zi/3)+(1-zi)/8.

Thanks.

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: Roger Stafford

Date: 13 Jul, 2010 20:09:04

Message: 6 of 14

"yang " <macmaster_egypt@yahoogroups.com> wrote in message <i1ifas$r3q$1@fred.mathworks.com>...
> Thanks for your quick reply,
>
> 1-let us called them
>
> > z1=( 0 0 1 1 1)
> > z2=(0 1 0 1 1)
> > z3=(0 1 1 0 1)
> > z4=(0 1 1 1 0)
> > z5=(1 0 0 1 1)
> > z6=(1 0 1 0 1)
> > z7=(1 0 1 1 0)
> > z8=(1 1 0 0 1)
> > z9=( 1 1 0 1 0)
> > z10=(1 1 1 0 0)
> >
> How can i generate them?
> then I would like to sum over z1, z2,....,z10 for something like (zi/3)+(1-zi)/8.
>
> Thanks.
- - - - - - - -
  You still haven't made it clear what you mean by "sum over". Are you taking the sum separately along each column, or the sum of all the elements in each of the z's, or perhaps the sum of each row? Please make it clear what you are asking!

  Assuming you mean the sum for each column, then each of the sums,

 (zi/3)+(1-zi)/8 ,

would just be

 (r-1)!*ell/ell!/(r-ell)! * (1/3-1/8) + r!/ell!/(r-ell)!/8

What could be easier?

  If you actually want the set of z's generated for other purposes, see the FEX contribution by Jos:

 http://www.mathworks.com/matlabcentral/fileexchange/7147-combn

Roger Stafford

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: yang

Date: 13 Jul, 2010 21:36:04

Message: 7 of 14

I am sorry for confusion.

By sum over i mean the sum of each row. your answer will be changed?

Thanks

Subject: how to generate all vectors of length n with entries are 0's or 1's then sum over

From: Roger Stafford

Date: 13 Jul, 2010 22:32:07

Message: 8 of 14

"yang " <macmaster_egypt@yahoogroups.com> wrote in message <i1im84$41b$1@fred.mathworks.com>...
> I am sorry for confusion.
>
> By sum over i mean the sum of each row. your answer will be changed?
>
> Thanks
- - - - - - - - -
  I had the strong impression that that was what ell meant, the sum of each row. That is, if you have a row of numbers which are each 0 or 1 and ell of them are 1, then their sum is bound to be ell. Tell me if I my impression was wrong. If so, what do you mean by ell?

Roger Stafford

Subject: how to generate all vectors of length n with entries are 0's or

From: Nathan

Date: 13 Jul, 2010 22:49:07

Message: 9 of 14

On Jul 13, 2:36 pm, "yang " <macmaster_eg...@yahoogroups.com> wrote:
> I am sorry for confusion.
>
> By sum over i mean the sum of each row.  your answer will be changed?
>
> Thanks

A simple solution:

r = 5;
ell = 3;
count = 0;
for i=1:2^r-1
  tmp = dec2bin(i,5)-'0';
  if(sum(tmp)==ell)
    count=count+1;
    z(count,:) = tmp;
  end
end
%%%%%%%%%%%
z =
     0 0 1 1 1
     0 1 0 1 1
     0 1 1 0 1
     0 1 1 1 0
     1 0 0 1 1
     1 0 1 0 1
     1 0 1 1 0
     1 1 0 0 1
     1 1 0 1 0
     1 1 1 0 0

-Nathan

Subject: how to generate all vectors of length n with entries are 0's or

From: yang

Date: 13 Jul, 2010 23:38:05

Message: 10 of 14

Hi,
By ell i mean sum of elements of each vector like (1 0 0 1 1)==>n=5 and ell=1+0+0+1+1=3.

Subject: how to generate all vectors of length n with entries are 0's or

From: Bruno Luong

Date: 14 Jul, 2010 00:23:07

Message: 11 of 14

"yang " <macmaster_egypt@yahoogroups.com> wrote in message <i1itct$dgn$1@fred.mathworks.com>...
> Hi,
> By ell i mean sum of elements of each vector like (1 0 0 1 1)==>n=5 and ell=1+0+0+1+1=3.

r = 5;
ell = 3;

j = nchoosek(1:r,ell);
i = repmat((1:size(j,1))',1,size(j,2));
z = accumarray([i(:) j(:)],1)

% Bruno

Subject: how to generate all vectors of length n with entries are 0's or

From: Roger Stafford

Date: 14 Jul, 2010 00:41:05

Message: 12 of 14

"yang " <macmaster_egypt@yahoogroups.com> wrote in message <i1itct$dgn$1@fred.mathworks.com>...
> Hi,
> By ell i mean sum of elements of each vector like (1 0 0 1 1)==>n=5 and ell=1+0+0+1+1=3.
- - - - - - -
  Yang, in your first posting you said "So we would like to sum over theses z_r's." Subsequently I said "You still haven't made it clear what you mean by 'sum over'." You then said, "By sum over i mean the sum of each row." And later you said "By ell i mean sum of elements of each vector like (1 0 0 1 1)==>n=5 and ell=1+0+0+1+1=3."

  My question to you is this. If by ell you meant the sum of the elements in the vectors and if these vectors are row vectors, and if by "sum over" you meant the same thing, the sums of the rows, what was your purpose in putting this thread on CSSM? What is it you wanted to find out in the way of summing if ell was already your answer?

Roger Stafford

Subject: how to generate all vectors of length n with entries are 0's or

From: Nathan

Date: 14 Jul, 2010 18:57:07

Message: 13 of 14

On Jul 13, 5:41 pm, "Roger Stafford"
<ellieandrogerxy...@mindspring.com.invalid> wrote:
> "yang " <macmaster_eg...@yahoogroups.com> wrote in message <i1itct$dg...@fred.mathworks.com>...
> > Hi,
> > By ell i mean sum of elements of each vector like (1 0 0 1 1)==>n=5 and ell=1+0+0+1+1=3.
>
> - - - - - - -
>   Yang, in your first posting you said "So we would like to sum over theses z_r's."  Subsequently I said "You still haven't made it clear what you mean by 'sum over'."  You then said, "By sum over i mean the sum of each row."  And later you said "By ell i mean sum of elements of each vector like (1 0 0 1 1)==>n=5 and ell=1+0+0+1+1=3."
>
>   My question to you is this.  If by ell you meant the sum of the elements in the vectors and if these vectors are row vectors, and if by "sum over" you meant the same thing, the sums of the rows, what was your purpose in putting this thread on CSSM?  What is it you wanted to find out in the way of summing if ell was already your answer?
>
> Roger Stafford

If you read the title more clearly, you would notice that he asks "how
to generate all vectors of length n" with the sum of ell.
He just wants to know, based on a given vector length and sum of 1's,
how to generate these vectors.

-Nathan

Subject: how to generate all vectors of length n with entries are 0's or

From: Roger Stafford

Date: 14 Jul, 2010 19:34:05

Message: 14 of 14

Nathan <ngreco32@gmail.com> wrote in message <30247d60-dfed-4069-8172-49c3d6cc6ba4@n19g2000prf.googlegroups.com>...
> If you read the title more clearly, you would notice that he asks "how
> to generate all vectors of length n" with the sum of ell.
> He just wants to know, based on a given vector length and sum of 1's,
> how to generate these vectors.
>
> -Nathan
- - - - - - - -
  The subject text I read was "how to generate all vectors of length n with entries are 0's or 1's then sum over" with no mention of summing to ell. However, your explanation, Nathan, is the only one that makes sense, in which case I owe you an apology, Yang. Bruno's suggestion of nchoosek is a valid way to do your problem.

Roger Stafford

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