Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

Mathematical Operations

with Arrays
Presented to:
Ma’am Aymon Zahra

Presented by:
Asad Tanveer (2k21-CHE-143)
Generation of Random Numbers
• Simulations of many physical processes and engineering applications
frequently requires using a number (or a set of numbers) that has a
random value.
• MATLAB has two commands “rand” and “randn” used to assign
tandom numbers to variables.
The “randn” Command
• The rand command generates
uniformly distributed numbers
with values between 0 and 1.
• The command can be used to
assign these numbers to a
scalar, vector or matrix as shown
in fig.
Cont.
• Sometimes it is need to have random numbers distrubeted in interval
other than (0,1) or nos. that are only integers.
• To do so, Random nis, distributed in range (a,b) can be obtained by
multiplying rand by (b-a) and adding to product a:
(b-a)*rand+a
Cont.
• Random numbers that are all integers can be generated by using one
of the round function.
• Example: A 2-by-15 matrix with random integers with values that
range from 1 to 100 can be created by:
The “randn” Command:
• The “randn” command generates normally distributed numbers with
mean 0 and standard deviation of 1.
• The command can be used to generate a single number, a vector or a
matrix in the same way as the rand command.
• Example: A 3-by-4 matrix is obtained by:
Cont.
• The mean and standard deviation of the numbers can be changed by
mathematical operations to have any values.
• This is done by multiplying the number generated by the “randn”
function by desired stand deviation and adding the desired mean.
• Example: A vector of 10 numbers (integers) with a mean of 50 and
standard deviation of 5 is generated by:
Example 1:
Example 2
Example 3
Example 4

You might also like