生成具有最小元素间距离的随机序列(Matlab) [英] Generating random sequence with minimum distance between elements (Matlab)

查看:237
本文介绍了生成具有最小元素间距离的随机序列(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在给定的时间间隔上生成给定长度的随机数序列,其约束是两个数彼此之间的距离应不超过两个.生成数字的时间间隔将大于序列的长度乘以最小距离,但不会长很多,因此某些数字很可能会失败该条件.

I need to generate a sequence of random numbers of a given length, on a given interval, with the constraint that no two numbers should be within a certain distance of each other. The interval on which the numbers are generated would be larger than the length of the sequence multipied by the minimum distance, but not by much, so some numbers would likely fail the condition.

这可能是微不足道的,但是除了生成随机序列,然后遍历它来检查每对是否满足条件之外,我真的想不出一个解决此问题的好方法,如果不满足,则替换它们并再检查一遍.似乎太长了,因为无法保证新生成的数字会满足该条件,并且迭代本身可能会花费一些时间.

It might be trivial, but I can't really think of a good solution to this problem, other than generating the random sequence, then iterating through it to check if each pair meets the condition, and if not, replace them and check again. It seems way too long, since there is no guarantee that the newly generated number would meet the condition, and the iteration itself could take quite some time.

谁能想到更好的解决方案?

Can anyone think of a better solution?

推荐答案

很多可能的答案,具体取决于您的挑剔程度.如果您不挑剔,那就可以了

Lots of possible answers, depending on how picky you are. If you're not to picky, this would work

L=10;  %length of interval
d=1;   %minimum distance
N=9;   %number of points
E=L-(N-1)*d;  %excess space for points

%generate N+1 random values; 
Ro=rand(N+1,1);     %random vector
%normalize so that the extra space is consumed
% extra value is the amount of extra space "unused"
Rn=E*Ro(1:N)/sum(Ro); %normalize

%spacing of points
S=d*ones(N,1)+Rn;  

%location of points, adjusted to "start" at 0
P=cumsum(S)-1

典型的顺序是

P =[
   0.060612
   1.4073
   2.676
   3.7901
   4.9476
   6.0333
   7.2426
   8.5684
   9.9247];

这篇关于生成具有最小元素间距离的随机序列(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆