Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How can I do this in MATLAB? (Possibilities)
Date: Sat, 31 Jan 2009 15:14:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 45
Message-ID: <gm1prp$95f$1@fred.mathworks.com>
References: <gm1mkl$eq0$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1233414841 9391 172.30.248.38 (31 Jan 2009 15:14:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 31 Jan 2009 15:14:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: news.mathworks.com comp.soft-sys.matlab:515135

"Husam Aldahiyat" <numandina@gmail.com> wrote in message <gm1mkl$eq0$1@fred.mathworks.com>...
> Hello,
> Say I have an vector a=[0 0 0]; I want all the possibilities of switching the zeros to numbers. For example I want a function that grants the following as output:
> 
> Number of zeros: 3
> Max number sum: 3
> 
> Output:
> 0 0 0
> 1 0 0
> 0 1 0
> 0 0 1
> 2 0 0
> 1 1 0
> 0 2 0
> 0 1 1
> 0 0 2
> 1 0 1
> 3 0 0
> 2 1 0
> 2 0 1
> 0 3 0
> 1 2 0
> 0 2 1
> 0 0 3
> 0 1 2
> 1 0 2
> 1 1 1
> 
> Or at least get the answer for maximum sum of 1:
> 0 0 0
> 1 0 0
> 0 1 0
> 0 0 1
> 
> I think I can do it using dec2bin and some other operations but it would be extremely inefficient.
> Any help is welcome!

  I don't think 'dec2bin' would be of much use to you.  Instead, let n be the number of "zeros" and m the maximum sum, and do this:

 output = diff([zeros(nchoosek(n,n+m),1),nchoosek(1:n,n+m)])-1;

The output should be an (n+m)!/(n!*m!) by n array containing all the possibilities that you have listed, though not in the sequential order you have used.

Roger Stafford