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:
matrix permutation with block repetition

Subject: matrix permutation with block repetition

From: Andrea

Date: 4 Jan, 2012 13:53:08

Message: 1 of 5

Dear all,
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:

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
 

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.

Could someone give me a hand?

Thank you very much
Andrea

Subject: matrix permutation with block repetition

From: Roger Stafford

Date: 4 Jan, 2012 22:34:08

Message: 2 of 5

"Andrea" wrote in message <je1lk4$9rp$1@newscl01ah.mathworks.com>...
> 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.
- - - - - - - - -
  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.

 m = 7; n = 32;
 s = ceil(m*rand); % Select first index randomly
 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

 p = reshape(p,n,m).'; % Second step

 for k = 1:n
  r = randperm(m-1);
  r = r + (r>=s);
  p(1+m*(k-1):m*k) = p([s,r]+m*(k-1)); % Third step
 end

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

Roger Stafford

Subject: matrix permutation with block repetition

From: Andrea

Date: 4 Jan, 2012 23:29:08

Message: 3 of 5

"Roger Stafford" wrote in message <je2k50$r7h$1@newscl01ah.mathworks.com>...
> "Andrea" wrote in message <je1lk4$9rp$1@newscl01ah.mathworks.com>...
> > 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.
> - - - - - - - - -
> 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.
>
> m = 7; n = 32;
> s = ceil(m*rand); % Select first index randomly
> 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
>
> p = reshape(p,n,m).'; % Second step
>
> for k = 1:n
> r = randperm(m-1);
> r = r + (r>=s);
> p(1+m*(k-1):m*k) = p([s,r]+m*(k-1)); % Third step
> end
>
> % The permutation p is now fully prepared
> B = A(:,p); % Permute the columns of A with B as the result
>
> Roger Stafford

Thanks for your reply.
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.

For example:
      5 5 5 5 5 5 5 3 3 3 3 3 3 3
    10 15 15 25 20 20 25 15 20 20 25 15 20 10
    40 30 10 50 40 80 40 40 40 60 10 60 60 80
      1 1 1 1 1 1 1 1 1 1 1 1 1 1

Another thing is avoid repetition in the third row inside each block of 7 values, but is not fundamental.

Thank you.
Andrea

Subject: matrix permutation with block repetition

From: Roger Stafford

Date: 5 Jan, 2012 20:40:09

Message: 4 of 5

"Andrea" wrote in message <je2nc4$7fm$1@newscl01ah.mathworks.com>...
> For example:
> 5 5 5 5 5 5 5 3 3 3 3 3 3 3
> 10 15 15 25 20 20 25 15 20 20 25 15 20 10
> 40 30 10 50 40 80 40 40 40 60 10 60 60 80
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- - - - - - - - - - -
  Latest attempt:

 m = 7; n = 25;
 [~,p] = sort(rand(n,m),1);
 B = A(p+repmat(n*(randperm(m)-1),n,1)); % <-- Could also use bsxfun here

  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.

  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.

Roger Stafford

Subject: matrix permutation with block repetition

From: Andrea

Date: 5 Jan, 2012 22:55:09

Message: 5 of 5

"Roger Stafford" wrote in message <je51r9$sj6$1@newscl01ah.mathworks.com>...

> Latest attempt:
>
> m = 7; n = 25;
> [~,p] = sort(rand(n,m),1);
> B = A(p+repmat(n*(randperm(m)-1),n,1)); % <-- Could also use bsxfun here
>
> 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.
>
> 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.
>
> Roger Stafford

Thanks Roger.

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:
block length = 6 so
for each first row number I will have 6 blocks and in total I will have 42 blocks.


In any case, thank you very much for your help. If I find the solution I'll post it.

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