Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Permutatation of given Matrix with no row repetition

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 14 Dec, 2011 11:42:09

Message: 1 of 11

Dear all

I have a 4x224 matrix that looks like this:

1 1 1 ... 2 2 2 ... 3 3 ...
12 14 16 ... 12 14 16 ...12 14 ...
34 44 54 ... 34 44 54 ... 34 44 ...
2 2 2 ... 2 2 2 ... 2 2 ...

In the first row I have number from 1 to 7
In the second row I have 4 different values and in the third row I have 8 different values
The basic no random matrix accounts for all the combination (224)

If I randperm the matrix I get all the columns beautifully randomised but the problem is that I
I want to avoid adjacent repetitions in the first row considering a range of 7 values.

So considering the first row 1 3 4 5 2 7 6 is fine but 2 3 2 5 4 7 6 is not

Could someone give me a hand

Thank you very much
Andrea

Subject: Permutatation of given Matrix with no row repetition

From: Torsten

Date: 14 Dec, 2011 12:48:15

Message: 2 of 11

On 14 Dez., 12:42, "Andrea " <de...@inwind.it> wrote:
> Dear all
>
> I have a 4x224 matrix that looks like this:
>
> 1    1    1  ... 2   2   2   ... 3   3 ...
> 12 14 16  ... 12 14 16 ...12 14 ...
> 34 44 54  ... 34 44 54 ... 34 44 ...
> 2    2   2   ...  2   2   2  ... 2   2 ...
>
> In the first row I have number from 1 to 7
> In the second row I have 4 different values and in the third row I have 8 different values
> The basic no random matrix accounts for all the combination (224)
>
> If I randperm the matrix  I get all the columns beautifully randomised but the problem is that I
> I want to avoid adjacent repetitions in the first row considering a range of 7 values.
>
> So considering the first row 1 3 4 5 2 7 6 is fine but 2 3 2 5 4 7 6 is not
>
> Could someone give me a hand
>
> Thank you very much
> Andrea

Choose 7 numbers randomly out of 1,...,32 with repetitions.
Say you got 23,14,4,9,17,17,1.
Remove column 23 of the columns with a 1 as first element, column 14
of the columns with a 2 as first element and so
on. Now make a random permutation of the numbers 1 - 7 and arrange the
7 extracted columns to a matrix
of size 4x7 according to the permutation.
Now the original matrix has size 4x217 (with the 7 columns removed).
Choose 7 numbers randomly out of 1,...31 with repetitions.
Continue as indicated above ...

Best wishes
Torsten.

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 14 Dec, 2011 18:13:08

Message: 3 of 11


> Choose 7 numbers randomly out of 1,...,32 with repetitions.
> Say you got 23,14,4,9,17,17,1.
> Remove column 23 of the columns with a 1 as first element, column 14
> of the columns with a 2 as first element and so
> on. Now make a random permutation of the numbers 1 - 7 and arrange the
> 7 extracted columns to a matrix
> of size 4x7 according to the permutation.
> Now the original matrix has size 4x217 (with the 7 columns removed).
> Choose 7 numbers randomly out of 1,...31 with repetitions.
> Continue as indicated above ...
>
> Best wishes
> Torsten.


Thanks Torsten

your idea is fine, but I don't know how to remove the extracted columns

thanks again

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 14 Dec, 2011 19:27:09

Message: 4 of 11

"Andrea" wrote in message <jcaovk$fg$1@newscl01ah.mathworks.com>...
>
> > Choose 7 numbers randomly out of 1,...,32 with repetitions.
> > Say you got 23,14,4,9,17,17,1.
> > Remove column 23 of the columns with a 1 as first element, column 14
> > of the columns with a 2 as first element and so
> > on. Now make a random permutation of the numbers 1 - 7 and arrange the
> > 7 extracted columns to a matrix
> > of size 4x7 according to the permutation.
> > Now the original matrix has size 4x217 (with the 7 columns removed).
> > Choose 7 numbers randomly out of 1,...31 with repetitions.
> > Continue as indicated above ...
> >
> > Best wishes
> > Torsten.

I tried your script but after the first part even if I do a random permutation of the numbers 1 - 7 and arrange the 7 extracted columns to a matrix of size 4x7 according to the permutation I can get two adjacent values: e.g. one from the last values of the first seven and one from the first value of the second block.

Subject: Permutatation of given Matrix with no row repetition

From: Roger Stafford

Date: 14 Dec, 2011 21:31:08

Message: 5 of 11

"Andrea" wrote in message <jca22g$bdf$1@newscl01ah.mathworks.com>...
> Dear all
>
> I have a 4x224 matrix that looks like this:
>
> 1 1 1 ... 2 2 2 ... 3 3 ...
> 12 14 16 ... 12 14 16 ...12 14 ...
> 34 44 54 ... 34 44 54 ... 34 44 ...
> 2 2 2 ... 2 2 2 ... 2 2 ...
>
> In the first row I have number from 1 to 7
> In the second row I have 4 different values and in the third row I have 8 different values
> The basic no random matrix accounts for all the combination (224)
>
> If I randperm the matrix I get all the columns beautifully randomised but the problem is that I
> I want to avoid adjacent repetitions in the first row considering a range of 7 values.
>
> So considering the first row 1 3 4 5 2 7 6 is fine but 2 3 2 5 4 7 6 is not
>
> Could someone give me a hand
>
> Thank you very much
> Andrea
- - - - - - - - - -
  Call the original 4 by 224 matrix A. If I have interpreted your description correctly, the first row of your desired result will be some permutation of the 7 possible numbers occurring in the first row of A, and after that this same permutation must be repeated 31 more times. Otherwise there would be some repetition of elements occurring within a 7-element interval. There are just 7! ways of doing this. Prior to performing this permutation, within each block of 32 in the original A any permutation of the columns in the block is permitted, making 32! possible permutations in each block. Combining these, there are therefore 7!*(32!)^7 possible acceptable permutations of the columns of A. That is a very large number (but not as large as 244!)

  In the following it is assumed that the first row of A is as you indicate with 32 repetitions of the first number, followed by 32 of the second number, then 32 of the third number, etc.

 m = 7; n = 32;
 B = A; % Allocate B
 for k = 1:m
  p = randperm(n);
  B(:,(1:n)+n*(k-1)) = A(:,p+n*(k-1));
 end
 p = randperm(m).';
 [a,b] = meshgrid(1:n,1:m);
 a = a(:); b = b(:);
 q = a + n*(p(b)-1);
 B = B(:,q); % <-- B is the desired result.

  In the above code the first action is to randomly permute each block of 32 columns, which can be done (32!)^7 ways. In the next part p is a random permutation of 1:7. It is used to rearrange the columns so that the first row consists of 32 repetitions of the permutation determined by p.

  (It will be easier to understand if you try it with smaller numbers for m and n.)

Roger Stafford

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 14 Dec, 2011 22:47:07

Message: 6 of 11

"Roger Stafford" wrote in message <jcb4is$cb2$1@newscl01ah.mathworks.com>...

> Call the original 4 by 224 matrix A. If I have interpreted your description correctly, the first row of your desired result will be some permutation of the 7 possible numbers occurring in the first row of A, and after that this same permutation must be repeated 31 more times. Otherwise there would be some repetition of elements occurring within a 7-element interval. There are just 7! ways of doing this. Prior to performing this permutation, within each block of 32 in the original A any permutation of the columns in the block is permitted, making 32! possible permutations in each block. Combining these, there are therefore 7!*(32!)^7 possible acceptable permutations of the columns of A. That is a very large number (but not as large as 244!)
>
> In the following it is assumed that the first row of A is as you indicate with 32 repetitions of the first number, followed by 32 of the second number, then 32 of the third number, etc.
>
> m = 7; n = 32;
> B = A; % Allocate B
> for k = 1:m
> p = randperm(n);
> B(:,(1:n)+n*(k-1)) = A(:,p+n*(k-1));
> end
> p = randperm(m).';
> [a,b] = meshgrid(1:n,1:m);
> a = a(:); b = b(:);
> q = a + n*(p(b)-1);
> B = B(:,q); % <-- B is the desired result.
>
> In the above code the first action is to randomly permute each block of 32 columns, which can be done (32!)^7 ways. In the next part p is a random permutation of 1:7. It is used to rearrange the columns so that the first row consists of 32 repetitions of the permutation determined by p.
>
> (It will be easier to understand if you try it with smaller numbers for m and n.)
>
> Roger Stafford

Thanks Roger
I understood your script, it's brilliant and it seems to work fine :9

Thank you

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 14 Dec, 2011 23:17:08

Message: 7 of 11

"Andrea" wrote in message <jcb91b$qnn$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <jcb4is$cb2$1@newscl01ah.mathworks.com>...
>
> > Call the original 4 by 224 matrix A. If I have interpreted your description correctly, the first row of your desired result will be some permutation of the 7 possible numbers occurring in the first row of A, and after that this same permutation must be repeated 31 more times. Otherwise there would be some repetition of elements occurring within a 7-element interval. There are just 7! ways of doing this. Prior to performing this permutation, within each block of 32 in the original A any permutation of the columns in the block is permitted, making 32! possible permutations in each block. Combining these, there are therefore 7!*(32!)^7 possible acceptable permutations of the columns of A. That is a very large number (but not as large as 244!)
> >
> > In the following it is assumed that the first row of A is as you indicate with 32 repetitions of the first number, followed by 32 of the second number, then 32 of the third number, etc.
> >
> > m = 7; n = 32;
> > B = A; % Allocate B
> > for k = 1:m
> > p = randperm(n);
> > B(:,(1:n)+n*(k-1)) = A(:,p+n*(k-1));
> > end
> > p = randperm(m).';
> > [a,b] = meshgrid(1:n,1:m);
> > a = a(:); b = b(:);
> > q = a + n*(p(b)-1);
> > B = B(:,q); % <-- B is the desired result.
> >
> > In the above code the first action is to randomly permute each block of 32 columns, which can be done (32!)^7 ways. In the next part p is a random permutation of 1:7. It is used to rearrange the columns so that the first row consists of 32 repetitions of the permutation determined by p.
> >
> > (It will be easier to understand if you try it with smaller numbers for m and n.)
> >
> > Roger Stafford

Sorry Roger, but I just noticed that I got the same pattern of 7 values in the first row along all the 224 trials and I want to avoid that. they need to be mixed inside the 7 and along the 224 without adjacent repetitions. Here an example of what I got:
  6 5 1 7 2 4 3 6 5 1 7 2 4 3
25 25 10 25 10 25 10 10 25 15 10 25 20 20
60 30 30 50 50 30 40 50 20 10 80 60 70 30
 1 1 1 1 1 1 1 1 1 1 1 1 1 1

but what i need is for example:

  6 5 1 7 2 4 3 7 2 4 3 6 5 1
25 25 10 25 10 25 10 10 25 15 10 25 20 20
60 30 30 50 50 30 40 50 20 10 80 60 70 30
 1 1 1 1 1 1 1 1 1 1 1 1 1 1
       
thank you very much
  
   
      
     

Subject: Permutatation of given Matrix with no row repetition

From: Roger Stafford

Date: 15 Dec, 2011 00:27:08

Message: 8 of 11

"Andrea" wrote in message <jcbapk$2hr$1@newscl01ah.mathworks.com>...
> Sorry Roger, but I just noticed that I got the same pattern of 7 values in the first row along all the 224 trials and I want to avoid that. they need to be mixed inside the 7 and along the 224 without adjacent repetitions.
> .....
> but what i need is for example:
> 6 5 1 7 2 4 3 7 2 4 3 6 5 1
- - - - - - - - -
  As I explained earlier, my code was specifically designed to do just that, and this means that I don't understand your original explanation. You stated that

 2 3 2 5 4 7 6

would not be suitable, and I presumed you meant that there was a repetition of the 2's within a seven-element interval. If this were your criterion, then after the first seven values are given in some permutation, all the remaining elements are uniquely determined just as in the case you found:

  6 5 1 7 2 4 3 6 5 1 7 2 4 3 ...

After the 7th element, 3, the only element that can follow without a repetition within a 7-element interval is the 6 again. The same applies to all remaining values.

  So, what is your criterion? In fully general terms, what determines when a sequence is suitable and when it is unsuitable? In your example

 6 5 1 7 2 4 3 7 2 4 3 6 5 1

the sequence 7 2 4 3 immediately repeats itself. On what grounds is this suitable? Please show several concrete examples of suitable and unsuitable sequences to illustrate this.

Roger Stafford
  

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 15 Dec, 2011 00:49:08

Message: 9 of 11

"Roger Stafford" wrote in message <jcbess$e3m$1@newscl01ah.mathworks.com>...
> "Andrea" wrote in message <jcbapk$2hr$1@newscl01ah.mathworks.com>...
> > Sorry Roger, but I just noticed that I got the same pattern of 7 values in the first row along all the 224 trials and I want to avoid that. they need to be mixed inside the 7 and along the 224 without adjacent repetitions.
> > .....
> > but what i need is for example:
> > 6 5 1 7 2 4 3 7 2 4 3 6 5 1
> - - - - - - - - -
> As I explained earlier, my code was specifically designed to do just that, and this means that I don't understand your original explanation. You stated that
>
> 2 3 2 5 4 7 6
>
> would not be suitable, and I presumed you meant that there was a repetition of the 2's within a seven-element interval. If this were your criterion, then after the first seven values are given in some permutation, all the remaining elements are uniquely determined just as in the case you found:
>
> 6 5 1 7 2 4 3 6 5 1 7 2 4 3 ...
>
> After the 7th element, 3, the only element that can follow without a repetition within a 7-element interval is the 6 again. The same applies to all remaining values.
>
> So, what is your criterion? In fully general terms, what determines when a sequence is suitable and when it is unsuitable? In your example
>
> 6 5 1 7 2 4 3 7 2 4 3 6 5 1
>
> the sequence 7 2 4 3 immediately repeats itself. On what grounds is this suitable? Please show several concrete examples of suitable and unsuitable sequences to illustrate this.
>
> Roger Stafford
>

You can think of having 32 block of 7 columns. Inside each block I need no repetitions inside each block so 1 3 4 5 2 7 6 is fine but 1 3 4 5 3 6 7 is not. In addition I have to use all the 224 values so I have 32 blocks like the one just describe. Now, considering two adjacent blocks I need again no repetition between the last value of the first block and the first value of the second. A sequence like this (1 3 4 5 2 7 6) (3 1 2 5 6 4 7) is fine, this one (1 3 4 5 2 7 6) (6 1 2 5 3 4 7) is not.

(I put the brackets just to divide the two blocks)

Subject: Permutatation of given Matrix with no row repetition

From: Roger Stafford

Date: 15 Dec, 2011 06:43:09

Message: 10 of 11

"Andrea" wrote in message <jcbg64$hjt$1@newscl01ah.mathworks.com>...
> You can think of having 32 block of 7 columns. Inside each block I need no repetitions inside each block so 1 3 4 5 2 7 6 is fine but 1 3 4 5 3 6 7 is not. In addition I have to use all the 224 values so I have 32 blocks like the one just describe. Now, considering two adjacent blocks I need again no repetition between the last value of the first block and the first value of the second. A sequence like this (1 3 4 5 2 7 6) (3 1 2 5 6 4 7) is fine, this one (1 3 4 5 2 7 6) (6 1 2 5 3 4 7) is not.
>
> (I put the brackets just to divide the two blocks)
- - - - - - - - - -
  See if this does what you want, Andrea. Again, A is the original 4 by 224 matrix.

 m = 7; n = 32;
 p = 1:m*n;
 for k = 1:m
  q = randperm(n);
  p(1+n*(k-1):n*k) = p(q+n*(k-1)); % First step
 end

 [a,b] = meshgrid(1:n,1:m);
 p = p(a+n*(b-1)); % Second step

 q = randperm(m);
 p(1:m) = p(q); % Third step, first m elements
 s = q(m);
 for k = 2:n
  t = ceil((m-1)*rand);
  s = t + (t>=s);
  r = randperm(m-1);
  r = r + (r>=s);
  q = [s,r];
  s = q(m);
  p(1+m*(k-1):m*k) = p(q+m*(k-1)); % Third step, remaining elements
 end

 % The permutation p is now fully prepared
 B = A(:,p); % Permute the columns of A with B as the result

Roger Stafford

Subject: Permutatation of given Matrix with no row repetition

From: Andrea

Date: 15 Dec, 2011 08:12:07

Message: 11 of 11

"Roger Stafford" wrote in message <jcc4tt$fg2$1@newscl01ah.mathworks.com>...
> "Andrea" wrote in message <jcbg64$hjt$1@newscl01ah.mathworks.com>...
> > You can think of having 32 block of 7 columns. Inside each block I need no repetitions inside each block so 1 3 4 5 2 7 6 is fine but 1 3 4 5 3 6 7 is not. In addition I have to use all the 224 values so I have 32 blocks like the one just describe. Now, considering two adjacent blocks I need again no repetition between the last value of the first block and the first value of the second. A sequence like this (1 3 4 5 2 7 6) (3 1 2 5 6 4 7) is fine, this one (1 3 4 5 2 7 6) (6 1 2 5 3 4 7) is not.
> >
> > (I put the brackets just to divide the two blocks)
> - - - - - - - - - -
> See if this does what you want, Andrea. Again, A is the original 4 by 224 matrix.
>
> m = 7; n = 32;
> p = 1:m*n;
> for k = 1:m
> q = randperm(n);
> p(1+n*(k-1):n*k) = p(q+n*(k-1)); % First step
> end
>
> [a,b] = meshgrid(1:n,1:m);
> p = p(a+n*(b-1)); % Second step
>
> q = randperm(m);
> p(1:m) = p(q); % Third step, first m elements
> s = q(m);
> for k = 2:n
> t = ceil((m-1)*rand);
> s = t + (t>=s);
> r = randperm(m-1);
> r = r + (r>=s);
> q = [s,r];
> s = q(m);
> p(1+m*(k-1):m*k) = p(q+m*(k-1)); % Third step, remaining elements
> end
>
> % The permutation p is now fully prepared
> B = A(:,p); % Permute the columns of A with B as the result
>
> Roger Stafford

Yes, It does. Thank you very very much

Andrea

Tags for this Thread

No tags are associated with this thread.

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.

Contact us