From: astro mmi on
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.
From: David Young on
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)
From: Roger Stafford on
"astro mmi" <pyarsa_madhu(a)yahoo.co.in> wrote in message <hqfo0t$71n$1(a)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
From: Roger Stafford on
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