Asked by Brwa
on 20 May 2013

Im new in matlab, i hope someone can help me with my problem. I need to make a code to solve this Cobinatoric example. I have a vector with 6 numbers where 2 of them are 1 and the rest are 0 as you can see below

A=[1 1 0 0 0 0]

I want to make a code which can help me change the position of all (1)s and put them in all the possible positions as you can see bellow without changing it all of them by myself, because my code is not only 5 numbers, i have a huge number.

A=[0 1 1 0 0 0]

A=[0 0 1 1 0 0]

A=[0 0 0 1 1 0]

A=[0 0 0 0 1 1]

A=[1 0 1 0 0 0]

A=[1 0 0 1 0 0]

A=[1 0 0 0 1 0]

A=[1 0 0 0 0 1]

A=[0 1 0 1 0 0]

A=[0 1 0 0 1 0]

A=[0 1 0 0 0 1]

A=[0 0 1 0 1 0]

A=[0 0 1 0 0 1]

A=[0 0 0 1 0 1]

Answer by Brwa
on 20 May 2013

Accepted answer

This answer was flagged by
Stephen Cobeldick

Dear; Andrei Bobrov and David Sanchez thank you so much for your help and for your time.

but i still have a problem that i can not use the first code by Andrei Bobrov for big vectors more than 8 values. and Im wondering if i can change number of (1)'s in the other 2 codes

such as A =[ 1 1 1 1 0 0 0 0 0 0]

I really think you guys are amazing and wish you all the best and happiness.

Brwa

Andrei Bobrov
on 20 May 2013

See EDIT part in my answer.

Answer by Andrei Bobrov
on 20 May 2013

Edited by Andrei Bobrov
on 20 May 2013

out = unique(perms([1 1 0 0 0 0]),'rows');

or [EDIT]

A = [1 1 1 1 0 0 0 0 0 0];

n = numel(A); b = nnz(A); M = 0:ones(1,n)*pow2(n-1:-1:0)'; z = rem(floor(M(:)*pow2(1-n:0)),2); out = z(sum(z,2) == b,:);

Brwa
on 20 May 2013

your code is very powerful, although icant understand it very clearly, but i checked some different values it was all correct.

Thank you very much

Brwa
on 5 Jun 2013

Dear Andrei Bobrov

is it possible to use that code for big numbers such as n => 30 and b => 5 ?

when i tried n = 25 and b =5 i got error said

??? Error using ==> mtimes Out of memory. Type HELP MEMORY for your options.

Error in ==> Position at 5

z = rem(floor(M(:)*pow2(1-n:0)),2);

and when i tried n = 30 i got this error

??? Error using ==> colon Maximum variable size allowed by the program is exceeded.

Error in ==> Position at 4

M = 0:ones(1,n)*pow2(n-1:-1:0)';

I wish you have a solution for this problem

Thanks for your help.

Triveni
on 31 Oct 2015

@Andrei Bobrov

If A= [-45 -45 -45 -45 -45 -45 0 0 0 0 0 0 0 0 45 45 45 45 45 45]

then how can be write??

Answer by David Sanchez
on 20 May 2013

Andrei's answer maybe the best choice, but in case you want to see what's behind:

A=zeros(1,6);

for k=1:size(A,2) A=zeros(1,6); A(k) = 1; for n = k+1:size(A,2) if n>k+1 A(n-1) = 0; end A(n) = 1 end end

Brwa
on 20 May 2013

thank you for your help. you both are amazing

Answer by Roger Stafford
on 20 May 2013

This problem is naturally made for Matlab's 'nchoosek' function. This method requires no rejection afterward and will therefore be easiest on your memory. Let A be a row vector of ones and zeros.

n = size(A,2); k = sum(A==1); C = nchoosek(1:n,k); m = size(C,1); % m will equal n!/k!/(n-k)! B = zeros(m,n); B(repmat((1-m:0)',1,k)+m*C) = 1; % Place ones according to indices in C

The desired position combinations will appear in the rows of B, with k ones in each row.

Note: Beware of "huge" numbers! If you have, say, 15 ones and 15 zeros in A, the number of possible arrangements of them is an enormous 155,117,520.

Related Content

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

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments