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