
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:(j1) > 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

