问题描述
我正在学习范德比尔特大学(Vanderbilt University)的一门有关Matlab基础知识的在线课程(关于使用MATLAB进行计算机编程入门的Coursera MOOC,讲师:Mike Fitzpatrick).在课程中,教授创建了一个名为myRand的自定义函数,以在设定的数字间隔内生成一个3×4矩阵.
I am following an online course from Vanderbilt University on the basics of Matlab (Coursera MOOC on introductory computer programming with MATLAB, Instructor: Mike Fitzpatrick). In the course the professor creates a custom function called myRand to produce a 3-by-4 matrix within a set interval of numbers.
我知道Matlab内置了相同的功能,但教授从头开始创建了自己的函数,以告诉我们内置函数的幕后情况.
I know that an identical function is built into Matlab but the professor created his own from scratch to teach us what is going on behind the scenes of the built-in functions.
% Produce a 3-by-4 matrix between between the input low and high
function a = myRand(low,high)
a = low + rand(3,4) * (high-low);
end
'>>test = myRand(2,10)
test =
5.1378 7.6484 2.3694 7.5586
7.2438 2.2547 2.7771 4.5368
3.3695 4.2154 8.5877 9.6018
我了解是因为rand()会产生0-1的随机数; "low"将设置下限(low + rand),这是有道理的.但是,我不知道如何设置上限. (高-低)"运算是高和低之间的差异.在"myRand"的任何地方都没有明确地设置"High"的规范.
I understand because rand() produces random numbers from 0-1; it makes sense that 'low' will set the lower limit (low+rand). However I do not know how upper limit is set. The operation '(high-low)' is the difference between the high and low. and no where in 'myRand' is there a specification to set 'High' explicitly.
有人可以帮助我理解这一点吗?
Can someone help me understand this?
推荐答案
说明示例:-
假设 low = 2
和 high = 5
.
现在您已经知道 rand
生成的值始终在 0
和 1
之间.
Suppose that low = 2
and high = 5
.
Now as you already know that the values generated by rand
are always between 0
and 1
.
如果生成的值为 0
,则
a = low + value *(high-low);
表示 a = 2 + 0 *(5-2) = 2
If the generated value is 0
then
a = low + value *(high-low);
means that a = 2 + 0 *(5-2) = 2
,并且当生成的值为 1
时,
a = low + value *(high-low);
表示 a = 2 + 1 *(5-2) = 5
and when the generated value is 1
then
a = low + value *(high-low);
means that a = 2 + 1 *(5-2) = 5
这意味着在 0和1 之间生成的任何值都会在 2和5
之间给出 a
.如果 values ∈ (0,1)
,则表示 a ∈ (2,5)
It means that any value generated in between 0 and 1 will give a
in between 2 and 5
i.e. if values ∈ (0,1)
then it means that a ∈ (2,5)
这篇关于在指定数字范围之间创建一个随机的X-Y矩阵.这里发生了什么事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!