Say, I have a matrix
a = [1 2 3 4 5 6 7 8 9 10]
randsel = randperm (10,5)
Say randsel returns [1 3 5 6 10]
Now, I want to select the rest of the numbers and put it into another matrix ,say 'randsel2'
How to do that??!

 Respuesta aceptada

Stephen23
Stephen23 el 8 de Nov. de 2015
Editada: Stephen23 el 8 de Nov. de 2015

1 voto

If you generate all of the indices using randperm, then you can simply select the first half of those indices and the second half: these are all random indices, so the two selections are also random.
>> a = 20:29 % twenties to distinguish from the indices.
a =
20 21 22 23 24 25 26 27 28 29
>> idx = randperm(10); % generate all indices
>> a(idx(1:5)) % first half of random indices to select one set
ans =
29 28 25 21 20
>> a(idx(6:end)) % second half of random indices to select reamining
ans =
22 27 26 23 24

1 comentario

Sarit  Hati
Sarit Hati el 8 de Nov. de 2015
Editada: Sarit Hati el 8 de Nov. de 2015
Thank you man!! Thanks a Lot!!
Any way i solved it differently!!(because i'm a noob)
Say tmp3=10 and tm4=5
randsel = randperm (tmp3,tmp4)
randsel2 = [1:1:tmp3];
for n=1:tmp4
for n1=1:tmp3
if randsel(n)==randsel2(n1);
randsel2(n1)=0;
end
end
end
But thanks anyway!!
Cheers!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Preguntada:

el 8 de Nov. de 2015

Editada:

el 8 de Nov. de 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by