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:
randomize a matrix

Subject: randomize a matrix

From: Pr B

Date: 22 Apr, 2009 23:29:01

Message: 1 of 5

hi all,

i need to randomize a matrix. that is, i need to switch the position of the elements of a matrix randomly. for example,

a = [ 1 0 ; 3 4]
new a = [ 0 4 ; 1 3]

is there a command that would enable me to do this on a matrix of 5000x4 dimension?

thanks!

Subject: randomize a matrix

From: Roger Stafford

Date: 22 Apr, 2009 23:39:01

Message: 2 of 5

"Pr B" <pb2297@columbia.edu> wrote in message <gso97t$csg$1@fred.mathworks.com>...
> i need to randomize a matrix. that is, i need to switch the position of the elements of a matrix randomly. for example,
>
> a = [ 1 0 ; 3 4]
> new a = [ 0 4 ; 1 3]
>
> is there a command that would enable me to do this on a matrix of 5000x4 dimension?

  Yes. Use 'randperm' to get a permutation of 1:5000*4 to use as an index into the matrix:

 p = randperm(5000*4);
 a = reshape(a(p),size(a));

Roger Stafford

Subject: randomize a matrix

From: Matt Fig

Date: 23 Apr, 2009 00:07:02

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message
> p = randperm(5000*4);
> a = reshape(a(p),size(a));


There is no need for reshape:

a = [ 1 0 ; 3 4];

 a(randperm(numel(a))) = a

Subject: randomize a matrix

From: Pr B

Date: 23 Apr, 2009 00:56:02

Message: 4 of 5

"Matt Fig" <spamanon@yahoo.com> wrote in message <gsobf6$912$1@fred.mathworks.com>...
> "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message
> > p = randperm(5000*4);
> > a = reshape(a(p),size(a));
>
>
> There is no need for reshape:
>
> a = [ 1 0 ; 3 4];
>
> a(randperm(numel(a))) = a

thanks all. worked like a charm.

Subject: randomize a matrix

From: Jos

Date: 23 Apr, 2009 06:21:03

Message: 5 of 5

"Pr B" <pb2297@columbia.edu> wrote in message <gso97t$csg$1@fred.mathworks.com>...
> hi all,
>
> i need to randomize a matrix. that is, i need to switch the position of the elements of a matrix randomly. for example,
>
> a = [ 1 0 ; 3 4]
> new a = [ 0 4 ; 1 3]
>
> is there a command that would enable me to do this on a matrix of 5000x4 dimension?
>
> thanks!

You might be interested in these two of my submissions:

RANDSWAP - randomly swap elements of a matrix
http://www.mathworks.com/matlabcentral/fileexchange/12621

and

SHAKE - Randomize a matrix along a specific dimension
http://www.mathworks.com/matlabcentral/fileexchange/10067

Jos

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