如何用边界制作随机点?

How to make random points with bounds?

我想在下限 [lx, ly, lz] 和上限 [ux, uy, uz] 之间创建 100 个随机 3 维点。

我使用的代码如下

noPoints = 100;
LB = [lx, ly, lz];
UB = [ux, uy, uz];
repmat(UB-LB,noPoints,1).*rand(noPoints,3) + repmat(LB,noPoints,1);

不过,我刚知道下面的代码

repmat(UB-LB,noPoints,1).*rand(noPoints,3) + LB;

给出相同的结果。 这让我意识到 "adding a row vector into a matrix is the same as adding the row vector into each row vectors of the matrix." 对于这里,我有了新的好奇。 有什么方法可以完全删除函数 "repmat" 吗? 实际上,

(UB-LB).*rand(noPoints,3) + LB;

无法正常工作,因为两个操作数的维度设置不正确。

有没有快速、干净、简单的代码来在边界约束下制作多个随机点??

解决方法:

(UB-LB).*rand(noPoints,3) + LB;

由于隐式扩展,适用于 R2016b 和更高版本的 MATLAB。

在以前的版本中,您可以使用 bsxfun :

bsxfun(@times, UB-LB, bsxfun(@plus,rand(noPoints,3) , LB))

从 MATLAB R2017b 开始,您可以使用 rescale :

rescale(rand(noPoints,3), LB, UB, 'InputMin', 0, 'InputMax', 1)