http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240
MATLAB Central Newsreader  Permutatation of given Matrix with no row repetition
Feed for thread: Permutatation of given Matrix with no row repetition
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 14 Dec 2011 11:42:09 +0000
Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861396
Andrea
Dear all<br>
<br>
I have a 4x224 matrix that looks like this:<br>
<br>
1 1 1 ... 2 2 2 ... 3 3 ...<br>
12 14 16 ... 12 14 16 ...12 14 ...<br>
34 44 54 ... 34 44 54 ... 34 44 ...<br>
2 2 2 ... 2 2 2 ... 2 2 ...<br>
<br>
In the first row I have number from 1 to 7<br>
In the second row I have 4 different values and in the third row I have 8 different values<br>
The basic no random matrix accounts for all the combination (224)<br>
<br>
If I randperm the matrix I get all the columns beautifully randomised but the problem is that I<br>
I want to avoid adjacent repetitions in the first row considering a range of 7 values.<br>
<br>
So considering the first row 1 3 4 5 2 7 6 is fine but 2 3 2 5 4 7 6 is not<br>
<br>
Could someone give me a hand<br>
<br>
Thank you very much<br>
Andrea

Wed, 14 Dec 2011 12:48:15 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861408
Torsten
On 14 Dez., 12:42, "Andrea " <de...@inwind.it> wrote:<br>
> Dear all<br>
><br>
> I have a 4x224 matrix that looks like this:<br>
><br>
> 1 1 1 ... 2 2 2 ... 3 3 ...<br>
> 12 14 16 ... 12 14 16 ...12 14 ...<br>
> 34 44 54 ... 34 44 54 ... 34 44 ...<br>
> 2 2 2 ... 2 2 2 ... 2 2 ...<br>
><br>
> In the first row I have number from 1 to 7<br>
> In the second row I have 4 different values and in the third row I have 8 different values<br>
> The basic no random matrix accounts for all the combination (224)<br>
><br>
> If I randperm the matrix I get all the columns beautifully randomised but the problem is that I<br>
> I want to avoid adjacent repetitions in the first row considering a range of 7 values.<br>
><br>
> So considering the first row 1 3 4 5 2 7 6 is fine but 2 3 2 5 4 7 6 is not<br>
><br>
> Could someone give me a hand<br>
><br>
> Thank you very much<br>
> Andrea<br>
<br>
Choose 7 numbers randomly out of 1,...,32 with repetitions.<br>
Say you got 23,14,4,9,17,17,1.<br>
Remove column 23 of the columns with a 1 as first element, column 14<br>
of the columns with a 2 as first element and so<br>
on. Now make a random permutation of the numbers 1  7 and arrange the<br>
7 extracted columns to a matrix<br>
of size 4x7 according to the permutation.<br>
Now the original matrix has size 4x217 (with the 7 columns removed).<br>
Choose 7 numbers randomly out of 1,...31 with repetitions.<br>
Continue as indicated above ...<br>
<br>
Best wishes<br>
Torsten.

Wed, 14 Dec 2011 18:13:08 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861446
Andrea
<br>
> Choose 7 numbers randomly out of 1,...,32 with repetitions.<br>
> Say you got 23,14,4,9,17,17,1.<br>
> Remove column 23 of the columns with a 1 as first element, column 14<br>
> of the columns with a 2 as first element and so<br>
> on. Now make a random permutation of the numbers 1  7 and arrange the<br>
> 7 extracted columns to a matrix<br>
> of size 4x7 according to the permutation.<br>
> Now the original matrix has size 4x217 (with the 7 columns removed).<br>
> Choose 7 numbers randomly out of 1,...31 with repetitions.<br>
> Continue as indicated above ...<br>
> <br>
> Best wishes<br>
> Torsten.<br>
<br>
<br>
Thanks Torsten<br>
<br>
your idea is fine, but I don't know how to remove the extracted columns<br>
<br>
thanks again

Wed, 14 Dec 2011 19:27:09 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861457
Andrea
"Andrea" wrote in message <jcaovk$fg$1@newscl01ah.mathworks.com>...<br>
> <br>
> > Choose 7 numbers randomly out of 1,...,32 with repetitions.<br>
> > Say you got 23,14,4,9,17,17,1.<br>
> > Remove column 23 of the columns with a 1 as first element, column 14<br>
> > of the columns with a 2 as first element and so<br>
> > on. Now make a random permutation of the numbers 1  7 and arrange the<br>
> > 7 extracted columns to a matrix<br>
> > of size 4x7 according to the permutation.<br>
> > Now the original matrix has size 4x217 (with the 7 columns removed).<br>
> > Choose 7 numbers randomly out of 1,...31 with repetitions.<br>
> > Continue as indicated above ...<br>
> > <br>
> > Best wishes<br>
> > Torsten.<br>
<br>
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.

Wed, 14 Dec 2011 21:31:08 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861470
Roger Stafford
"Andrea" wrote in message <jca22g$bdf$1@newscl01ah.mathworks.com>...<br>
> Dear all<br>
> <br>
> I have a 4x224 matrix that looks like this:<br>
> <br>
> 1 1 1 ... 2 2 2 ... 3 3 ...<br>
> 12 14 16 ... 12 14 16 ...12 14 ...<br>
> 34 44 54 ... 34 44 54 ... 34 44 ...<br>
> 2 2 2 ... 2 2 2 ... 2 2 ...<br>
> <br>
> In the first row I have number from 1 to 7<br>
> In the second row I have 4 different values and in the third row I have 8 different values<br>
> The basic no random matrix accounts for all the combination (224)<br>
> <br>
> If I randperm the matrix I get all the columns beautifully randomised but the problem is that I<br>
> I want to avoid adjacent repetitions in the first row considering a range of 7 values.<br>
> <br>
> So considering the first row 1 3 4 5 2 7 6 is fine but 2 3 2 5 4 7 6 is not<br>
> <br>
> Could someone give me a hand<br>
> <br>
> Thank you very much<br>
> Andrea<br>
         <br>
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 7element 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!)<br>
<br>
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.<br>
<br>
m = 7; n = 32;<br>
B = A; % Allocate B<br>
for k = 1:m<br>
p = randperm(n);<br>
B(:,(1:n)+n*(k1)) = A(:,p+n*(k1));<br>
end<br>
p = randperm(m).';<br>
[a,b] = meshgrid(1:n,1:m);<br>
a = a(:); b = b(:);<br>
q = a + n*(p(b)1);<br>
B = B(:,q); % < B is the desired result.<br>
<br>
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.<br>
<br>
(It will be easier to understand if you try it with smaller numbers for m and n.)<br>
<br>
Roger Stafford

Wed, 14 Dec 2011 22:47:07 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861485
Andrea
"Roger Stafford" wrote in message <jcb4is$cb2$1@newscl01ah.mathworks.com>...<br>
<br>
> 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 7element 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!)<br>
> <br>
> 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.<br>
> <br>
> m = 7; n = 32;<br>
> B = A; % Allocate B<br>
> for k = 1:m<br>
> p = randperm(n);<br>
> B(:,(1:n)+n*(k1)) = A(:,p+n*(k1));<br>
> end<br>
> p = randperm(m).';<br>
> [a,b] = meshgrid(1:n,1:m);<br>
> a = a(:); b = b(:);<br>
> q = a + n*(p(b)1);<br>
> B = B(:,q); % < B is the desired result.<br>
> <br>
> 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.<br>
> <br>
> (It will be easier to understand if you try it with smaller numbers for m and n.)<br>
> <br>
> Roger Stafford<br>
<br>
Thanks Roger <br>
I understood your script, it's brilliant and it seems to work fine :9<br>
<br>
Thank you

Wed, 14 Dec 2011 23:17:08 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861486
Andrea
"Andrea" wrote in message <jcb91b$qnn$1@newscl01ah.mathworks.com>...<br>
> "Roger Stafford" wrote in message <jcb4is$cb2$1@newscl01ah.mathworks.com>...<br>
> <br>
> > 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 7element 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!)<br>
> > <br>
> > 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.<br>
> > <br>
> > m = 7; n = 32;<br>
> > B = A; % Allocate B<br>
> > for k = 1:m<br>
> > p = randperm(n);<br>
> > B(:,(1:n)+n*(k1)) = A(:,p+n*(k1));<br>
> > end<br>
> > p = randperm(m).';<br>
> > [a,b] = meshgrid(1:n,1:m);<br>
> > a = a(:); b = b(:);<br>
> > q = a + n*(p(b)1);<br>
> > B = B(:,q); % < B is the desired result.<br>
> > <br>
> > 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.<br>
> > <br>
> > (It will be easier to understand if you try it with smaller numbers for m and n.)<br>
> > <br>
> > Roger Stafford<br>
<br>
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:<br>
6 5 1 7 2 4 3 6 5 1 7 2 4 3 <br>
25 25 10 25 10 25 10 10 25 15 10 25 20 20<br>
60 30 30 50 50 30 40 50 20 10 80 60 70 30<br>
1 1 1 1 1 1 1 1 1 1 1 1 1 1<br>
<br>
but what i need is for example:<br>
<br>
6 5 1 7 2 4 3 7 2 4 3 6 5 1<br>
25 25 10 25 10 25 10 10 25 15 10 25 20 20<br>
60 30 30 50 50 30 40 50 20 10 80 60 70 30<br>
1 1 1 1 1 1 1 1 1 1 1 1 1 1<br>
<br>
thank you very much<br>
<br>
<br>
<br>

Thu, 15 Dec 2011 00:27:08 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861488
Roger Stafford
"Andrea" wrote in message <jcbapk$2hr$1@newscl01ah.mathworks.com>...<br>
> 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.<br>
> .....<br>
> but what i need is for example:<br>
> 6 5 1 7 2 4 3 7 2 4 3 6 5 1<br>
        <br>
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<br>
<br>
2 3 2 5 4 7 6 <br>
<br>
would not be suitable, and I presumed you meant that there was a repetition of the 2's within a sevenelement 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:<br>
<br>
6 5 1 7 2 4 3 6 5 1 7 2 4 3 ...<br>
<br>
After the 7th element, 3, the only element that can follow without a repetition within a 7element interval is the 6 again. The same applies to all remaining values.<br>
<br>
So, what is your criterion? In fully general terms, what determines when a sequence is suitable and when it is unsuitable? In your example<br>
<br>
6 5 1 7 2 4 3 7 2 4 3 6 5 1<br>
<br>
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.<br>
<br>
Roger Stafford<br>

Thu, 15 Dec 2011 00:49:08 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861489
Andrea
"Roger Stafford" wrote in message <jcbess$e3m$1@newscl01ah.mathworks.com>...<br>
> "Andrea" wrote in message <jcbapk$2hr$1@newscl01ah.mathworks.com>...<br>
> > 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.<br>
> > .....<br>
> > but what i need is for example:<br>
> > 6 5 1 7 2 4 3 7 2 4 3 6 5 1<br>
>         <br>
> 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<br>
> <br>
> 2 3 2 5 4 7 6 <br>
> <br>
> would not be suitable, and I presumed you meant that there was a repetition of the 2's within a sevenelement 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:<br>
> <br>
> 6 5 1 7 2 4 3 6 5 1 7 2 4 3 ...<br>
> <br>
> After the 7th element, 3, the only element that can follow without a repetition within a 7element interval is the 6 again. The same applies to all remaining values.<br>
> <br>
> So, what is your criterion? In fully general terms, what determines when a sequence is suitable and when it is unsuitable? In your example<br>
> <br>
> 6 5 1 7 2 4 3 7 2 4 3 6 5 1<br>
> <br>
> 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.<br>
> <br>
> Roger Stafford<br>
> <br>
<br>
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.<br>
<br>
(I put the brackets just to divide the two blocks)

Thu, 15 Dec 2011 06:43:09 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861506
Roger Stafford
"Andrea" wrote in message <jcbg64$hjt$1@newscl01ah.mathworks.com>...<br>
> 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.<br>
> <br>
> (I put the brackets just to divide the two blocks)<br>
         <br>
See if this does what you want, Andrea. Again, A is the original 4 by 224 matrix.<br>
<br>
m = 7; n = 32;<br>
p = 1:m*n;<br>
for k = 1:m<br>
q = randperm(n);<br>
p(1+n*(k1):n*k) = p(q+n*(k1)); % First step<br>
end<br>
<br>
[a,b] = meshgrid(1:n,1:m);<br>
p = p(a+n*(b1)); % Second step<br>
<br>
q = randperm(m);<br>
p(1:m) = p(q); % Third step, first m elements<br>
s = q(m);<br>
for k = 2:n<br>
t = ceil((m1)*rand);<br>
s = t + (t>=s);<br>
r = randperm(m1);<br>
r = r + (r>=s);<br>
q = [s,r];<br>
s = q(m);<br>
p(1+m*(k1):m*k) = p(q+m*(k1)); % Third step, remaining elements<br>
end<br>
<br>
% The permutation p is now fully prepared<br>
B = A(:,p); % Permute the columns of A with B as the result<br>
<br>
Roger Stafford

Thu, 15 Dec 2011 08:12:07 +0000
Re: Permutatation of given Matrix with no row repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315240#861514
Andrea
"Roger Stafford" wrote in message <jcc4tt$fg2$1@newscl01ah.mathworks.com>...<br>
> "Andrea" wrote in message <jcbg64$hjt$1@newscl01ah.mathworks.com>...<br>
> > 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.<br>
> > <br>
> > (I put the brackets just to divide the two blocks)<br>
>          <br>
> See if this does what you want, Andrea. Again, A is the original 4 by 224 matrix.<br>
> <br>
> m = 7; n = 32;<br>
> p = 1:m*n;<br>
> for k = 1:m<br>
> q = randperm(n);<br>
> p(1+n*(k1):n*k) = p(q+n*(k1)); % First step<br>
> end<br>
> <br>
> [a,b] = meshgrid(1:n,1:m);<br>
> p = p(a+n*(b1)); % Second step<br>
> <br>
> q = randperm(m);<br>
> p(1:m) = p(q); % Third step, first m elements<br>
> s = q(m);<br>
> for k = 2:n<br>
> t = ceil((m1)*rand);<br>
> s = t + (t>=s);<br>
> r = randperm(m1);<br>
> r = r + (r>=s);<br>
> q = [s,r];<br>
> s = q(m);<br>
> p(1+m*(k1):m*k) = p(q+m*(k1)); % Third step, remaining elements<br>
> end<br>
> <br>
> % The permutation p is now fully prepared<br>
> B = A(:,p); % Permute the columns of A with B as the result<br>
> <br>
> Roger Stafford<br>
<br>
Yes, It does. Thank you very very much <br>
<br>
Andrea