Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: How to speed creating of matrix of permutations
Replies: 9   Last Post: Aug 13, 2013 3:05 PM

 Messages: [ Previous | Next ]
 Nasser Abbasi Posts: 6,677 Registered: 2/7/05
Re: How to speed creating of matrix of permutations
Posted: Aug 9, 2013 11:17 AM

On 8/9/2013 10:01 AM, Emma wrote:
> Hi,
>
> I'm trying to create a marix filled with 0s and 1s, where each column is a unique arrangement
>of 0s and 1s (with no constraint on the number of 0s or 1s in each column).
>Therefore, [0;0;0;0] [1;1;1;1] and [0;1;1;0] would all be possible columns.
> Since I'm trying to create a matrix that contains all of the possible
>permutations of 0s and 1s, the matrix should be of size (n, 2^n).
>
> I currently have the following code for creating the matrix (below).
> At the moment, the code runs fairly quickly when n=4, takes about a day
>when n=16, but is far too slow when n gets larger. I would currently like to create a matrix where n=24.
>
> The code that I'm using is as follows...
>
> randmat = zeros(n,2^n);
> for j=1:size(randmat,2)
> run = 1;
> while run
> determine_run = 0;
> for i = 1:(j-1)
> randmat(:,j) = rand(n,1)>0.5;
> % Check that the values added to the permutation matrix do
> % not match any that are already present.
> while all(randmat(:,j)==randmat(:,i))
> determine_run = 1;
> randmat(:,j) = rand(n,1)>0.5;
> end
> end
> if ~determine_run
> run = 0;
> end
> end
> end
>
> Can anyone help me to find a quicker way of implementing this?
>
> Many thanks,
> Emma
>

Your code has a bug, column 2 = column 6

EDU>> n=3;
randmat =

0 1 0 0 0 1 1 1
0 1 1 0 1 1 0 0
0 0 1 1 0 0 1 0

column 1=column4=column8 :

randmat =

0 0 0 0 0 1 1 0
0 1 0 0 1 1 0 0
0 0 1 0 1 1 1 0

--Nasser

Date Subject Author
8/9/13 Emma
8/9/13 Nasser Abbasi
8/9/13 Emma
8/9/13 wicore
8/9/13 Marc
8/9/13 wicore
8/10/13 Marc
8/9/13 Marc
8/9/13 Emma
8/13/13 Steven Lord