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:
Shuffle or randomize the data in the row

Subject: Shuffle or randomize the data in the row

From: Syed Imam

Date: 5 Nov, 2012 18:08:09

Message: 1 of 4

>> A=(1:5)

A =

     1 2 3 4 5

HOW TO SHUFFLE THE ABOVE GENERATED DATA IN A ROW AND re-SHUFFLE
IN OTHER WORDS, what function will be use to randomize and de-randomize

lets suppose:
generated data [ 1 2 3 4 5 ]
shuffled data [ 3 5 1 2 4]... WHAT FUNCTION TO USE ?
re-shuffled data [ 1 2 3 4 5] ... WHAT FUNCTION TO USE ?


               

Subject: Shuffle or randomize the data in the row

From: someone

Date: 5 Nov, 2012 18:51:08

Message: 2 of 4

"Syed Imam" <nasmc92@hotmail.co.uk> wrote in message <k78va9$n23$1@newscl01ah.mathworks.com>...
> >> A=(1:5)
>
> A =
>
> 1 2 3 4 5
>
> HOW TO SHUFFLE THE ABOVE GENERATED DATA IN A ROW AND re-SHUFFLE
> IN OTHER WORDS, what function will be use to randomize and de-randomize
>
> lets suppose:
> generated data [ 1 2 3 4 5 ]
A = [1 2 3 4 5];
> shuffled data [ 3 5 1 2 4]... WHAT FUNCTION TO USE ?
doc randperm
B = randperm(A)
> re-shuffled data [ 1 2 3 4 5] ... WHAT FUNCTION TO USE ?
B = A
>
>
>

Subject: Shuffle or randomize the data in the row

From: someone

Date: 5 Nov, 2012 20:00:08

Message: 3 of 4

"someone" wrote in message <k791qs$47i$1@newscl01ah.mathworks.com>...
> "Syed Imam" <nasmc92@hotmail.co.uk> wrote in message <k78va9$n23$1@newscl01ah.mathworks.com>...
> > >> A=(1:5)
> >
> > A =
> >
> > 1 2 3 4 5
> >
> > HOW TO SHUFFLE THE ABOVE GENERATED DATA IN A ROW AND re-SHUFFLE
> > IN OTHER WORDS, what function will be use to randomize and de-randomize
> >
> > lets suppose:
> > generated data [ 1 2 3 4 5 ]
> A = [1 2 3 4 5];
> > shuffled data [ 3 5 1 2 4]... WHAT FUNCTION TO USE ?
> doc randperm
> B = randperm(A)
% Sorry, did that a little too fast, should be:
B = A(randperm(length(A)))
% Split the last line up & save the indices if you want
> > re-shuffled data [ 1 2 3 4 5] ... WHAT FUNCTION TO USE ?
> B = A
> >
> >
> >

Subject: Shuffle or randomize the data in the row

From: Roger Stafford

Date: 5 Nov, 2012 20:12:10

Message: 4 of 4

"Syed Imam" <nasmc92@hotmail.co.uk> wrote in message <k78va9$n23$1@newscl01ah.mathworks.com>...
> generated data [ 1 2 3 4 5 ]
> shuffled data [ 3 5 1 2 4]... WHAT FUNCTION TO USE ?
> re-shuffled data [ 1 2 3 4 5] ... WHAT FUNCTION TO USE ?
- - - - - - - - -
  If by "shuffle" you actually mean a permutation in which each element should be replaced by a different element, that is called a "derangement". A routine to generate random derangements can be obtained at the File Exchange:

 http://www.mathworks.com/matlabcentral/fileexchange/30189

 n = length(A);
 p = randpermfull(n);
 B = A(p);

To recover A from B, you need p:

 C = zeros(size(B));
 C(p) = B;

or if you actually need the inverse of p, do this:

 q = 1:n;
 q(p) = q;
 C = B(q);

  (It would be a lot easier to just save A.)

Roger Stafford

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