【发布时间】:2019-06-12 19:56:17
【问题描述】:
我遇到过这段代码,
基本上我需要生成两个随机数向量,
这是代码,
T = 500
e = normrnd(0,1,[2*T,k]);
T = size(e,1);
e = [e(ceil(rand(T,1)*T),:);e];
为什么这个方法与 ceil 一起使用?关于为什么有人会使用这种方法的任何想法?
【问题讨论】:
我遇到过这段代码,
基本上我需要生成两个随机数向量,
这是代码,
T = 500
e = normrnd(0,1,[2*T,k]);
T = size(e,1);
e = [e(ceil(rand(T,1)*T),:);e];
为什么这个方法与 ceil 一起使用?关于为什么有人会使用这种方法的任何想法?
【问题讨论】:
1 开始,ceil() 将转换0.8 to 1
ceil() 总是返回整数 >> x = rand(3,1)*5
x =
4.3117
4.4820
0.9451
>> ceil(x)
ans =
5
5
1
ceil(rand(m,n)*imax) and randi([1, imax], [m, n]) are equivalent.
要确认使用ceil() 的偏好,应该查看这些方法文档,例如在命令窗口中输入open ceil
rand()Copyright 1984-2017 The MathWorks, Inc.
ceil()Copyright 1984-2005 The MathWorks, Inc.
randi()Copyright 2008-2013 The MathWorks, Inc.
在早期的 Matlab 版本中,1984~2008a、randi() 不可用。
唯一的选择是使用rand() and ceil() or rand() and floor() 来生成均匀分布的随机整数。
简而言之,使用
ceil() and rand()可以生成随机数 所有 Matlab 版本,而randi()仅适用于 2008b 版本之后的版本。
【讨论】:
randi。我猜程序员不知道这个功能,并试图推出自己的功能。
randi was introduced in R2008b.