E.15 Statistical Functions 647
Table E.5 The different formats for using the rand function
Y = rand(n) Returns a square n ×n matrix of random
entries
Y = rand(m, n) Returns an m × n matrix of random entries
Y = rand(m, n, p,...) Generates random arrays
Y = rand(size(A)) Returns an array of random entries that is the
same size as matrix A
Table E.6 The different formats for using the hist function
HIST(Y ) Draws a histogram of the elements in Y
HIST(Y, x) Draws a histogram using n bins, where n is
length(x). x also specifies the locations on
the x-axis where hist places the bins.
returns an m ×n matrix of random numbers having a uniform distribution between
0 and 1. The command
x = randn
(
m, n
)
returns an m ×n matrix of random numbers having a Gaussian distribution between
0 and 1 with zero mean and unity variance. Table E.5 explains the different ways
the rand function could be invoked.
The command
x = randperm
(
n
)
;
returns a random permutation of the integers 1 to n.
To plot the pdf of a random number sequence, the hist and stairs functions
could be used. Table E.6 explains the different ways the hist function could be
invoked. For example, if x is a 5-element vector, HIST distributes the elements of
Y into five bins centered on the x-axis at the elements in x.
Example E.1 The following MATLAB code generates a Gaussian random variable
Y with mean 0 and standard deviation 1. The number of samples will be denoted
by n. The resulting data are plotted in one curve and the histogram is plotted in an
adjacent window.
n=1000; %number of samples
y=randn(1,n);
%plot theresulting dat
subplot(1,2,1)
plot(y);
box off
axis square
xlabel(’Sample index’)
ylabel(’Randomnumber value’)