Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

binominal counter

0 views
Skip to first unread message

Robert Schuetz

unread,
Jul 4, 2007, 2:31:59 PM7/4/07
to
dear matlab users,

I need to call a function with all combinations of 10 numbers. this
means:
1,2,3,4,5,6,7,8,9,10,[1,2],[1,3],[1,4],...,[1,2,3,4,5,6,7,8,9,10].
thus all binominal combinations given a binominal distribution or
all the possibilities to select k numbers out of N (sequence
unimportant) =: (N k) = N!/(k!*(N-k)!).

given 10 numbers I have 2^10 combinations, 1024.

how can I do this in matlab?
is there a matlab function to do this?
or do I have to program this loop by myself, and if so any tricks or
shortcuts you know of in order to program this as efficiently as
possible?

thanks a lot, Robert

Walter Roberson

unread,
Jul 4, 2007, 3:14:08 PM7/4/07
to
In article <ef5c7...@webcrossing.raydaftYaTP>,
Robert Schuetz <sch...@imsb.biol.ethz.ch> wrote:

>I need to call a function with all combinations of 10 numbers. this
>means:
>1,2,3,4,5,6,7,8,9,10,[1,2],[1,3],[1,4],...,[1,2,3,4,5,6,7,8,9,10].
>thus all binominal combinations given a binominal distribution or
>all the possibilities to select k numbers out of N (sequence
>unimportant) =: (N k) = N!/(k!*(N-k)!).

>given 10 numbers I have 2^10 combinations, 1024.

>how can I do this in matlab?

N = 10;
elements = 1:N;
combs = [];
for K = 1:N
combs = [combs; num2cell(nchoosek(elements,K),2)];
end
combs0 = cell(1,1024);
combs0{2:1024} = combs;

combs0 will now be a cell array of length 1024, each
member of which is an array containing one of the combinations.

combs by itself will be everything except the empty array. Starting
K at 0 instead of 1 would not help, as the empty array will be
silently squeezed out when you append the first item to the list.

--
All is vanity. -- Ecclesiastes

Jos

unread,
Jul 4, 2007, 3:26:10 PM7/4/07
to
Store the outcomes of NCHOOSEK in a cell array, like this:

V = 1:4 ;
N = numel(V) ;
RES = {} ;
for i=1:numel(V),
X = nchoosek(V,i) ;
RES = [RES ; mat2cell(X,ones(1,size(X,1)))] ;
end

hth
Jos

Steven Lord

unread,
Jul 4, 2007, 6:51:53 PM7/4/07
to

"Robert Schuetz" <sch...@imsb.biol.ethz.ch> wrote in message
news:ef5c7...@webcrossing.raydaftYaTP...

> dear matlab users,
>
> I need to call a function with all combinations of 10 numbers. this
> means:
> 1,2,3,4,5,6,7,8,9,10,[1,2],[1,3],[1,4],...,[1,2,3,4,5,6,7,8,9,10].
> thus all binominal combinations given a binominal distribution or
> all the possibilities to select k numbers out of N (sequence
> unimportant) =: (N k) = N!/(k!*(N-k)!).
>
> given 10 numbers I have 2^10 combinations, 1024.

1023, unless you also want the empty combination.

> how can I do this in matlab?
> is there a matlab function to do this?
> or do I have to program this loop by myself, and if so any tricks or
> shortcuts you know of in order to program this as efficiently as
> possible?

A = dec2bin(1:1023, 10)=='1';
b = 1:10;

Now use the rows of A as indices into b to extract individual combinations:

b(A(5, :))
b(A(182, :))
b(A(874, :))

or compute this matrix and take rows of the result, eliminating any 0's:

c = A*diag(1:10)

--
Steve Lord
sl...@mathworks.com


0 new messages