33262

Creating a random X-by-Y matrix between a range of specified numbers. What's happening here?

Question:

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.

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

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 <em>difference</em> between the high and low. and no where in 'myRand' is there a specification to set 'High' explicitly.

Can someone help me understand this?

Answer1:

<strong>Explanation with an example:-</strong>

Suppose that <strong>low = 2</strong> and <strong>high = 5</strong>. <br />Now as you already know that the values generated by <strong>rand</strong> are always between <strong>0</strong> and <strong>1</strong>.

If the generated value is <strong>0</strong> then <br /><strong>a = low + value *(high-low);</strong> means that <strong>a = 2 + 0 *(5-2) = 2</strong>

and when the generated value is <strong>1</strong> then <br /><strong>a = low + value *(high-low);</strong> means that <strong>a = 2 + 1 *(5-2) = 5</strong>

It means that any value generated in between <em>0 and 1</em> will give <strong>a</strong> in between <em>2 and 5</em> <br />i.e. if <strong>values ∈ (0,1)</strong> then it means that <strong>a ∈ (2,5)</strong>

Recommend

  • Returning a value at random based on a probability weights [duplicate]
  • How to pass a custom object in REST webservice
  • How to correctly deal with escaped Unicode Characters in R's library RJSONIO when reading json
  • pandas parse csv with left and right quote chars
  • setTimeOut and local function
  • What are zone turns?
  • Edit assembly language code in Visual Studio while stepping through each statement
  • MySql - get days remaining
  • Jhipster: How to create relationships with User entity using supplied tools?
  • data.table replicate rows after join?
  • How do I compile a C/C++ program through windows command prompt?
  • How to get the index of element in the List in c#
  • FTS3 searches in ORMLite?
  • Not able to display correct data in table -AngularJS
  • Less Conflicting Session Manager for Zope 2
  • Runtime.exec() gives Error: Could not find or load main class
  • there is no graph with tensorboard
  • Multiple producers single consumer locking schema
  • quiver not drawing arrows just lots of blue, matlab
  • How do I display a dialog that asks the user multi-choice questıon using tkInter?
  • What is the purpose of TaskExecutor in spring?
  • Access Android Market through SSH tunnel
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Moving mysql files across servers
  • ImageMagick, replace semi-transparent white with opaque white
  • Splitting given String into two variables - php
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • Build own AppleScript numerical error handling
  • How do I use the BLAS library provided by MATLAB?
  • Adding custom controls to a full screen movie
  • Why winpcap requires both .lib and .dll to run?
  • Proper folder structure for lots of source files
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery
  • Qt: Run a script BEFORE make
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • reshape alternating columns in less time and using less memory
  • Net Present Value in Excel for Grouped Recurring CF