Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,647
Registered: 2/7/05
Re: How to speed creating of matrix of permutations
Posted: Aug 9, 2013 11:17 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.