Statistical Analysis in MATLAB (2)
Statistical Analysis in MATLAB (2)
Uniform distribution
Normal distribution
Linear congruential method
3
%Examples on the use of random (pseudorandom) numbers
%Generate a standard normal random number (mean=0, std=1)
R = randn
%Generate a 3 by 3 matrix of standard normal random numbers
R = randn(3)
%Generate a 3 by 2 matrix of standard normal random numbers
R = randn(3,2)
%Generate an m by n by p by... matrix of standard normal random numbers
%R = randn(m,n,p,...)
%Generate standard normal random numbers with specific mean and variance:
meanR = 0.6; varR = 0.1;
R = meanR + sqrt(varR) * randn(4)
4
function x = LinConGen(m,a,c,x0,n)
% Returns the linear congruential sequence
5
%Generate uniform random M =
numbers between a and b:
a = 10; b = 60; 33.4878
R = a + (b-a)*rand(100,1) Me =
34.3208
M = mean(R)
Mo =
Me = median(R) 10.5168
Mo = mode(R)
S =
S = std(R) 13.8899
V = var(R)
V =
192.9298 6
x = randn(30,4); % Uncorrelated data
x(:,4) = sum(x,2); % Introduce correlation.
7
%hist(x,y) plots the histogram of values in x in
length(y) bins with centers specified by y.
%To close all open figures: y = [25,45,55,65,75,85,95,125]
close all figure(2), hist(x,y)
%histc(x,y) returns number of values in vector x
%Use the LinConGen function to generate 1000 that fall between the elements in y (must be
random values: monotonically nondecreasing values).
x = LinConGen(137,25,16,86,1000) [n, bin] = histc(x,y)
%Find the cumulative number of elements:
%You can find the mean and standard
deviation of the random values: cumulative_n = cumsum(n)
mean(x) %The cumulative histogram:
std(x)
figure(3), bar(y,cumulative_n)
%[n,xout] = hist(...) returns vectors n and xout
%hist(x) plots the histogram of x in 10 containing the frequency
bins.
%hist(x,n) plots the histogram of x in n %counts and the bin locations.
bins.
[n, xout] = hist(x,20)
figure(1), subplot(2,1,1), hist(x)
figure(4), subplot(2,1,1), bar(xout,n)
subplot(2,1,2), hist(x,20)
subplot(2,1,2), barh(xout,n)