我想在指定范围(包括具有整数间隔的端点)上生成随机数的nx1向量。例如
[0 120] with spacing 40
例如,如果n为10,则这样的输出可能是:
[0 80 0 120 0 0 40 0 120 40]
似乎randi()
是我应该使用的,但似乎不可能用它来做我想做的所有事情。还有其他功能可以帮助我吗?
答案 0 :(得分:3)
假设您拥有工具箱,请使用randsample
v=0:40:120 %possible values
n=10 %number of samples
randsample(v,n,true)
答案 1 :(得分:2)
您可以这样做:
randi( [0, floor(upperValue/spacing)], n, 1) * spacing
如果n = 10
,upperValue = 120
和spacing = 40
,您将得到:
ans =
0
80
0
80
120
120
80
80
40
40
如果floor
不是整数,则需要使用upperValue/spacing
。如果使用ceil(lowerValue/spacing)
,如果下限不为0,则可以执行类似的操作。