MATLAB Concepts
MATLAB Concepts
Y = round(X)
Matlab has a set of dot operators, such as .*, ./, .^. Each of these operators consists of a dot and a normal algebraic operator. They perform element-wise algebraic operations on a matrix. For example, consider the following codes
A = [1 2 3; 3 2 1]; x = [1 2 4]; B = A.^2 y = 1./x
The result is
B = 1 9 y = 1.0000 0.5000 0.2500 4 4 9 1
A sequence of values that appears to be random (i.e. unpredictable) but is actually generated by a deterministic algorithm.
>> rand('seed',0) >> rand(2) ans = 0.2190 0.6789 0.0470 0.6793 >> rand('seed',0) >> rand(2) ans = 0.2190 0.6789 0.0470 0.6793
>> rand('seed',1) >> rand(2) ans = 0.5129 0.3504 0.4605 0.0950 >> rand('seed',1) >> rand(2) ans = 0.5129 0.3504 0.4605 0.0950
randn
Normally distributed pseudorandom numbers
r = randn(n)
returns an n-by-n matrix containing pseudorandom values drawn from the standard normal distribution.
r = randn(m,n)
r = randn(m,n,p,...) r = randn
returns a scalar. returns an array the same size as A. or r = randn(...,'single') returns an array of normal values of
r = randn(size(A))
r = randn(...,'double')
Example 1
Generate values from a normal distribution with mean 1 and standard deviation 2:
r = 1 + 2.*randn(100,1);
Example 2
Generate values from a bivariate normal distribution with specified mean vector and covariance matrix:
mu = [1 2]; Sigma = [1 .5; .5 2]; R = chol(Sigma); z = repmat(mu,100,1) + randn(100,2)*R;
===================================================
L = chol(A,'lower') produces a lower triangular matrix L from the diagonal and lower triangle of
matrix A, satisfying the equation L*L'=A. The chol function assumes that A is (complex Hermitian) symmetric. If it is not, chol uses the (complex conjugate) transpose of the lower triangle as the upper triangle. When A is sparse, this syntax of chol is typically faster. Matrix A must be positive definite. R = chol(A,'upper') is the same as R = chol(A).
abs
Absolute value (magnitude)
hist
Histogram plot
hist(data)
creates a histogram bar plot of data. Elements in data are sorted into 10 equally spaced bins along the x-axis between the minimum and maximum values of data. Bins are displayed as rectangles such that the height of each rectangle indicates the number of elements in the bin. example
The values in data are sorted among 10 equally spaced bins between 0 and 10, the minimum and maximum values.
hist(data,nbins)
example
Specify Number of Bins
Initialize the random-number generator to make the output of randn repeatable. Generate 1,000 normally distributed pseudorandom numbers.
rng(0,'twister') data = randn(1000,1);
hist(data,xvalues) uses the values in vector xvalues to determine the bin intervals and sorts data into the number of bins determined by length(xvalues). To specify the bin centers, set xvalues equal to a vector of evenly spaced values. The first and last bins extend to cover the minimum and maximum values in data.
example
Specify Bin Intervals
Create a figure divided into three subplots. Plot a histogram of the same data set in all three subplots using different bin intervals for each histogram. First, initialize the random-number generator to make the output of randn repeatable. Generate 1,000 normally distributed pseudorandom numbers.
rng(0,'twister') data = randn(1000,1);
In the upper subplot specify the bin centers using a vector of evenly spaced values that span the values in data.
figure subplot(3,1,1) xvalues1 = -4:4; hist(data,xvalues1)
In the middle subplot specify the bin centers using a vector of evenly spaced values that do not span the values in data. Notice that the first and last bins extend to cover the minimum and maximum values in data.
subplot(3,1,2)
In the lower subplot set the bin intervals using a vector of unevenly spaced values. These unevenly spaced values are not used as the bin centers. MATLAB indicates the specified values by markers along the x-axis.
subplot(3,1,3) xvalues3 = [-4,-2.5,0,0.5,1,3]; hist(data,xvalues3)
hist(axes_handle,___) plots into the axes specified by axes_handle instead of into current axes (gca). The option axes_handle can precede any of the input argument
the
each bin.
nelements Number of elements in each bin row vector centers Bin centers row vector [nelements,centers] = hist(___) returns an additional row vector, centers, indicating the location of each bin center on the x-axis. To plot the histogram, you can use bar(centers,nelements).
example
Initialize the random-number generator to make the output of randn repeatable. Generate 1,000 normally distributed pseudorandom numbers.
rng(0,'twister') data = randn(1000,1);
Sort data into 10 equally spaced bins. Get the number of elements in each bin and the locations of the bin centers.
[nelements,centers] = hist(data) nelements = 4 27 88 190 270 243 123 38 centers = -2.8915 -2.2105 -1.5294 -0.8484 -0.1673 1.8758 2.5568 3.2379
13
4 0.5137 1.1947
If you want to avoid repeating the same random number arrays when MATLAB restarts, then execute the command,
rng('shuffle');
before calling rand, randn, randi, or randperm to ensure that you do not repeat a result from a previous MATLAB session. There also can be situations in which you want to repeat a result that you got at the start of a MATLAB session, but you do not want to restart to do it. You can reset the generator to the startup state at any time in a MATLAB session:
rng('default');
When you execute rng('default'), the ensuing random number commands return results that match the output of a new MATLAB session.
subplot
Description
subplot(m,n,p)
divides the current figure into an m-by-n grid and creates an axes in the grid position specified by p. MATLAB numbers its grids by row, such that the first grid is the first column of the first row, the second grid is the second column of the first row, and so on. example
subplot(m,n,p,'replace') subplot(m,n,p,'align')
deletes any existing axes in grid location p and creates a new axes.
creates a new axes so that the plot boxes are aligned. This is the
1.0.
If the position vector specifies an axes that overlaps any previous axes, then the new axes replaces the existing ones. example
subplot(___,Name,Value)
specifies properties for the axes using any of the input argument combinations in the previous syntaxes and one or more Name,Value pair arguments.
example
h = subplot(___)
example
subplot(h)
makes the axes with handle h the current axes for subsequent plotting commands.
bar
Bar graph
bar(Y)
example
bar(x,Y)
example
bar(___,width)
sets the relative bar width and controls the separation of bars within a group and can include any of the input arguments in previous syntaxes. example
bar(___,style)
specifies the style of the bars and can include any of the input arguments in previous syntaxes. example
bar(___,bar_color) displays all bars using the color specified by the single-letter of bar_color and can include any of the input arguments in previous syntaxes.
abbreviation
example
bar(___,Name,Value)
sets the property names to the specified values and can include any of the input arguments in previous syntaxes.