Thread Subject: Matrix composed of only ones and zeros

Subject: Matrix composed of only ones and zeros

From: Andrew Park

Date: 10 Mar, 2009 04:25:02

Message: 1 of 4

Hello.
I'm trying to build a matrix that is composed of unique permutations of only ones and zeros.
It's related to an old thread about "Unique permutation of [k1 k2 k3 k4 k5 0 0 0 0 0 0 0 0 0 ...]" but not exactly the same.
The matrix that I want is the following.
Assuming that I want a matrix of 4 columns, then,
[0,0,0,0; 1,0,0,0; 0,1,0,0; 0,0,1,0; 0,0,0,1; 1,1,0,0; 1,0,1,0; 1,0,0,1; 1,1,1,0; ... ;
1,1,1,1]
It's easy if the number of columns is 4 but what I need is when the number of columns is greater than 10.
I tried to do this by using perms() function but realized that this function doesn't work when the columns are greater than 8.
Here's my code. Does anybody have any ideas?

N=10;
ONE0=zeros(1,N);
ONE1_N=zeros(N,N);
for i=1:N
    for j=1:i
    ONE1_N(i,j)=1;
    end
end
ONES_ZEROS=ONE0;
for i=1:N
    temp=ONE1_N(i,:);
    temp=perms(temp);
    ONE_I=unique(temp,'rows');
    ONES_ZEROS=[ONES_ZEROS; ONE_I];
end

Subject: Matrix composed of only ones and zeros

From: James Tursa

Date: 10 Mar, 2009 04:36:01

Message: 2 of 4

"Andrew Park" <peachtree@dreamwiz.com> wrote in message <gp4q2u$1p1$1@fred.mathworks.com>...
> Hello.
> I'm trying to build a matrix that is composed of unique permutations of only ones and zeros.
> It's related to an old thread about "Unique permutation of [k1 k2 k3 k4 k5 0 0 0 0 0 0 0 0 0 ...]" but not exactly the same.
> The matrix that I want is the following.
> Assuming that I want a matrix of 4 columns, then,
> [0,0,0,0; 1,0,0,0; 0,1,0,0; 0,0,1,0; 0,0,0,1; 1,1,0,0; 1,0,1,0; 1,0,0,1; 1,1,1,0; ... ;
> 1,1,1,1]
> It's easy if the number of columns is 4 but what I need is when the number of columns is greater than 10.
> I tried to do this by using perms() function but realized that this function doesn't work when the columns are greater than 8.
> Here's my code. Does anybody have any ideas?
>
> N=10;
> ONE0=zeros(1,N);
> ONE1_N=zeros(N,N);
> for i=1:N
> for j=1:i
> ONE1_N(i,j)=1;
> end
> end
> ONES_ZEROS=ONE0;
> for i=1:N
> temp=ONE1_N(i,:);
> temp=perms(temp);
> ONE_I=unique(temp,'rows');
> ONES_ZEROS=[ONES_ZEROS; ONE_I];
> end

This should work, but at some value of N it will exceed your memory (results are in a different order than your example):

double(dec2bin(0:2^N-1)-'0')

James Tursa

Subject: Matrix composed of only ones and zeros

From: John D'Errico

Date: 10 Mar, 2009 08:43:28

Message: 3 of 4

"James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <gp4qnh$d9v$1@fred.mathworks.com>...
> "Andrew Park" <peachtree@dreamwiz.com> wrote in message <gp4q2u$1p1$1@fred.mathworks.com>...
> > Hello.
> > I'm trying to build a matrix that is composed of unique permutations of only ones and zeros.
>

(snip)

> This should work, but at some value of N it will exceed your memory (results are in a different order than your example):
>
> double(dec2bin(0:2^N-1)-'0')

Actually, the double is not needed. Subtracting the
'0' turns it into a double already.

John

Subject: Matrix composed of only ones and zeros

From: Andrew Park

Date: 10 Mar, 2009 17:36:02

Message: 4 of 4

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gp597g$46f$1@fred.mathworks.com>...
> "James Tursa" <aclassyguywithaknotac@hotmail.com> wrote in message <gp4qnh$d9v$1@fred.mathworks.com>...
> > "Andrew Park" <peachtree@dreamwiz.com> wrote in message <gp4q2u$1p1$1@fred.mathworks.com>...
> > > Hello.
> > > I'm trying to build a matrix that is composed of unique permutations of only ones and zeros.
> >
>
> (snip)
>
> > This should work, but at some value of N it will exceed your memory (results are in a different order than your example):
> >
> > double(dec2bin(0:2^N-1)-'0')
>
> Actually, the double is not needed. Subtracting the
> '0' turns it into a double already.
>
> John

Thanks, John!!!

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com