How to use randperm with minimum spacing between random numbers

6 visualizaciones (últimos 30 días)
Gordon Ariho
Gordon Ariho el 30 de Ag. de 2019
Comentada: Guillaume el 9 de Mayo de 2020
Hi
I would like to use randperm but be able to specify the minimum separation between the random numbers generated. Please advise.
p = randperm(n,k) returns a row vector containing k unique integers selected randomly from 1 to n inclusive.
I would like to have the k intergers to have atleast a separation of say b when sorted.
Thanks
Gordon
  1 comentario
Guillaume
Guillaume el 30 de Ag. de 2019
What sort of values are n, k and b? If n>>k and n>>b then you could just retry randperm until you've got a set that satisfies your condition.

Iniciar sesión para comentar.

Respuestas (2)

Bruno Luong
Bruno Luong el 30 de Ag. de 2019
Editada: Bruno Luong el 30 de Ag. de 2019
n=20;
k=3;
b=7;
[as,is]=sort(randperm(n-(k-1)*(b-1),k)); % if it throws an error then b/k/n are not compatible
a = as + (0:k-1)*(b-1);
a = a(is)
  2 comentarios
Mohd Faisal Ansari
Mohd Faisal Ansari el 9 de Mayo de 2020
Editada: Mohd Faisal Ansari el 9 de Mayo de 2020
Hello Sir, The above code is for the range from 0 to n
please help me to edit the code so that it works between range a to b
Thank you
Guillaume
Guillaume el 9 de Mayo de 2020
Use the exact same code to get numbers between 0 and (b-a), then add a to all the numbers, presto, you have numbers between a and b.

Iniciar sesión para comentar.


James Tursa
James Tursa el 30 de Ag. de 2019
Maybe something like this will suffice for your needs?
p = b * randperm(floor(n/b),k)
If n/b isn't an integer value, then there be some "missing" higher numbers. (Or could adjust this so the "missing" numbers are the lower numbers or are perhaps randomly scattered throughout the range).
  1 comentario
Mohd Faisal Ansari
Mohd Faisal Ansari el 9 de Mayo de 2020
Hello Sir, The above code is for the range from 0 to n
please help me to edit the code so that it works between range a to b
Thank you.

Iniciar sesión para comentar.

Categorías

Más información sobre Sparse Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by