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:
randperm

Subject: randperm

From: astro mmi

Date: 18 Apr, 2010 19:54:05

Message: 1 of 4

Hi everyone,
   I have two matrices
   x=[12 1 9 6 87 43 11 98];
   y=[32 44 4 5 6 7 14 11];
Say I want to randomly select a matrix z=[1 9 87 98] which is a subset of x. Also, I want the elements from y which have the same index as the elements selected from x i.e., t=[44 4 6 11].
I tried using randperm but the results are really not like the case explained above. Pls let me know if i am using the right command or how do i have 2 do it otherwise. thanx in advance.

Subject: randperm

From: David Young

Date: 18 Apr, 2010 20:08:21

Message: 2 of 4

randperm is the right tool - but maybe you aren't using it the way you need to. The following code does what I think you want:

x=[12 1 9 6 87 43 11 98];
y=[32 44 4 5 6 7 14 11];
needed = 4;
ind = randperm(length(x));
selected = ind(1:needed);
xselected = x(selected)
yselected = y(selected)

Subject: randperm

From: Roger Stafford

Date: 18 Apr, 2010 20:15:21

Message: 3 of 4

"astro mmi" <pyarsa_madhu@yahoo.co.in> wrote in message <hqfo0t$71n$1@fred.mathworks.com>...
> Hi everyone,
> I have two matrices
> x=[12 1 9 6 87 43 11 98];
> y=[32 44 4 5 6 7 14 11];
> Say I want to randomly select a matrix z=[1 9 87 98] which is a subset of x. Also, I want the elements from y which have the same index as the elements selected from x i.e., t=[44 4 6 11].
> I tried using randperm but the results are really not like the case explained above. Pls let me know if i am using the right command or how do i have 2 do it otherwise. thanx in advance.

 p = randperm(8);
 p = p(1:4);
 z = x(p);
 t = y(p);

Did you want the count 4 to be fixed or randomly determined also. If the latter, that can also be done using 'rand': "count = ceil(8*rand);".

Roger Stafford

Subject: randperm

From: Roger Stafford

Date: 18 Apr, 2010 21:00:24

Message: 4 of 4

  If each subset is to be equally probable then do this:

 p = 1:8;
 p = p(rand(1,8)<1/2);
 p = p(randperm(length(p)));
 z = x(p);
 t = y(p);

Roger Stafford

Tags for 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