http://www.mathworks.com/matlabcentral/newsreader/view_thread/315693
MATLAB Central Newsreader  matrix permutation with block repetition
Feed for thread: matrix permutation with block repetition
enus
©19942014 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, 04 Jan 2012 13:53:08 +0000
matrix permutation with block repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315693#862816
Andrea
Dear all,<br>
a month ago I asked for a scripts that generates random permutation of columns of a given matrix. A receive a script from Roger Stafford that working really good. The script is:<br>
<br>
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>
<br>
Now I need to generate a new matrix from the same values, but I need 32 block of 7 random columns each where the first value is always the same. In addition I have to use all the value in the matrix.<br>
<br>
Could someone give me a hand?<br>
<br>
Thank you very much<br>
Andrea

Wed, 04 Jan 2012 22:34:08 +0000
Re: matrix permutation with block repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315693#862870
Roger Stafford
"Andrea" wrote in message <je1lk4$9rp$1@newscl01ah.mathworks.com>...<br>
> Now I need to generate a new matrix from the same values, but I need 32 block of 7 random columns each where the first value is always the same. In addition I have to use all the value in the matrix.<br>
        <br>
Andrea, if I have understood you correctly the following should do what you want. I have assumed that your "first value" is to be selected randomly out of the 7 possible values. If instead you wish to specify it, just replace the "s = ceil(m*rand);" line with s set equal to your choice.<br>
<br>
m = 7; n = 32;<br>
s = ceil(m*rand); % Select first index randomly<br>
p = 1:m*n;<br>
<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>
p = reshape(p,n,m).'; % Second step<br>
<br>
for k = 1:n<br>
r = randperm(m1);<br>
r = r + (r>=s);<br>
p(1+m*(k1):m*k) = p([s,r]+m*(k1)); % Third step<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

Wed, 04 Jan 2012 23:29:08 +0000
Re: matrix permutation with block repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315693#862872
Andrea
"Roger Stafford" wrote in message <je2k50$r7h$1@newscl01ah.mathworks.com>...<br>
> "Andrea" wrote in message <je1lk4$9rp$1@newscl01ah.mathworks.com>...<br>
> > Now I need to generate a new matrix from the same values, but I need 32 block of 7 random columns each where the first value is always the same. In addition I have to use all the value in the matrix.<br>
>         <br>
> Andrea, if I have understood you correctly the following should do what you want. I have assumed that your "first value" is to be selected randomly out of the 7 possible values. If instead you wish to specify it, just replace the "s = ceil(m*rand);" line with s set equal to your choice.<br>
> <br>
> m = 7; n = 32;<br>
> s = ceil(m*rand); % Select first index randomly<br>
> p = 1:m*n;<br>
> <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>
> p = reshape(p,n,m).'; % Second step<br>
> <br>
> for k = 1:n<br>
> r = randperm(m1);<br>
> r = r + (r>=s);<br>
> p(1+m*(k1):m*k) = p([s,r]+m*(k1)); % Third step<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>
Thanks for your reply.<br>
Actually what I need it's a little bit different. Once generated the first value (randomly, like you did, is ok) I need to repeat the same first value for 7 times and then change it. As usual I have to use all the value in the matrix.<br>
<br>
For example:<br>
5 5 5 5 5 5 5 3 3 3 3 3 3 3<br>
10 15 15 25 20 20 25 15 20 20 25 15 20 10<br>
40 30 10 50 40 80 40 40 40 60 10 60 60 80<br>
1 1 1 1 1 1 1 1 1 1 1 1 1 1<br>
<br>
Another thing is avoid repetition in the third row inside each block of 7 values, but is not fundamental.<br>
<br>
Thank you.<br>
Andrea

Thu, 05 Jan 2012 20:40:09 +0000
Re: matrix permutation with block repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315693#862969
Roger Stafford
"Andrea" wrote in message <je2nc4$7fm$1@newscl01ah.mathworks.com>...<br>
> For example:<br>
> 5 5 5 5 5 5 5 3 3 3 3 3 3 3<br>
> 10 15 15 25 20 20 25 15 20 20 25 15 20 10<br>
> 40 30 10 50 40 80 40 40 40 60 10 60 60 80<br>
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1<br>
          <br>
Latest attempt:<br>
<br>
m = 7; n = 25;<br>
[~,p] = sort(rand(n,m),1);<br>
B = A(p+repmat(n*(randperm(m)1),n,1)); % < Could also use bsxfun here<br>
<br>
I'm losing confidence in being able to figure out what is desired here. In your most recent example the 5's in the first row appeared only 7 times rather than 32 as in the original 315240 thread. That would appear to give you 49 columns rather than 224. I have assumed that this is in error. If not, then I still don't understand what the problem is.<br>
<br>
In any case hopefully these various methods might serve to give you ideas for what is needed so that you can code it for yourself.<br>
<br>
Roger Stafford

Thu, 05 Jan 2012 22:55:09 +0000
Re: matrix permutation with block repetition
http://www.mathworks.com/matlabcentral/newsreader/view_thread/315693#862978
Andrea
"Roger Stafford" wrote in message <je51r9$sj6$1@newscl01ah.mathworks.com>...<br>
<br>
> Latest attempt:<br>
> <br>
> m = 7; n = 25;<br>
> [~,p] = sort(rand(n,m),1);<br>
> B = A(p+repmat(n*(randperm(m)1),n,1)); % < Could also use bsxfun here<br>
> <br>
> I'm losing confidence in being able to figure out what is desired here. In your most recent example the 5's in the first row appeared only 7 times rather than 32 as in the original 315240 thread. That would appear to give you 49 columns rather than 224. I have assumed that this is in error. If not, then I still don't understand what the problem is.<br>
> <br>
> In any case hopefully these various methods might serve to give you ideas for what is needed so that you can code it for yourself.<br>
> <br>
> Roger Stafford<br>
<br>
Thanks Roger.<br>
<br>
In my last explanation I wrote the 5's only 7 times just as an example but I you are right: I still need 32 5's, but divided in blocks. You are right in saying that 7 is not the right number because 32 is not divisible by 7. Basically what I need is to start from the 224 numbers and divided them in a certain number of blocks each of which composed by the same first row values. For instance:<br>
block length = 6 so<br>
for each first row number I will have 6 blocks and in total I will have 42 blocks.<br>
<br>
<br>
In any case, thank you very much for your help. If I find the solution I'll post it.