Random selection of n values from a vector - which function should I use?
17 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Karolina
el 29 de En. de 2016
Respondida: Walter Roberson
el 29 de En. de 2016
I have a vector with twenty unique values from 1 to 20. I would like to select randomly two values from my vector. Which option should I use? Is there some difference between the functions attached below?
randperm
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20];
y = A(randperm(20,2));
randsample
y = randsample(20,2);
datasample
y = datasample(A,2);
0 comentarios
Respuesta aceptada
Walter Roberson
el 29 de En. de 2016
randsample() requires the Stats toolbox, and has more options such as selection with replacement.
Since your source is [1:20], you can simplify your randperm version to just
y = randperm(20,2);
but if it did not happen to be consecutive integers you could use the form you did,
A = 1:20;
y = A(randperm(numel(A),2));
0 comentarios
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!