You are on page 1of 3

MATLAB, which stands for MATrix LABoratory, is a state-of-the-art

mathematical software package, that is used extensively in both academia


and industry.
whos gives all sorts of information on what variables are active
who , only provides the names of the variables that are active.
clear variable_name delete specific variable.
clear deletes all the variables from the memory
Cls clears the command window
u = [0:8]
v = [0:2:8] The two colons ( : ) tell MATLAB to fill in the (omitted) entries
using the specified increment value.
V(1:2) parentheses, instead of brackets, to refer to the entries of the vector.
(remember to separate the entries either by commas or spaces) and you
separate the rows by semicolons ( ; ).
A = [1 2 3; 3 4 5; 6 7 8]
Return carriage may be used to avoid Semicolon.
A(:,2)
A(3,:)
ans =
6
7
8
The table summarizes the operators that are available in MATLAB.
+
Addition
Subtraction
*

Multiplication

^
power
'
transpose
sqrt
square root functiom
MATLAB functions operate essentially on vectors returning a scalar value.
o max largest component
o min smallest component
o length
length of a vector
o sort sort in ascending order
o sum sum of elements
o prod product of elements
o median median value
o mean mean value
o std
standard deviation
Suppose we wanted to find the maximum element in the following matrix.
M = [ 0.7012,0.2625,0.3282 0.9103,0.0475,0.6326
0.7622,0.7361,0.7564]
M= 0.7012 0.2625 0.3282 0.9103 0.0475 0.6326
0.7361 0.7564

0.7622

If we used the max command on M, we will get the row in which the
maximum element lies (remember the vector functions act on matrices
in a column-by-column fashion).
max(M)
ans = 0.9103 0.7361 0.7564
To isolate the largest element, we must use the max command on the
above row vector. Taking advantage of the fact that MATLAB assigns
the variable name ans to the answer we obtained, we can simply type
max(ans)
ans = 0.9103
The two steps above can be combined into one in the following.
18
max(max(M))
ans = 0.9103
Combining MATLAB commands can be very useful when programming
complex
Matrix Functions
o Much of MATLABs power comes from its matrix functions. These can
be further separated into two sub-categories. The first one consists of
convenient matrix building functions, some of which are given in the
table below.
o eye identity matrix zeros matrix of zeros ones matrix of ones diag
extract diagonal of a matrix or create diagonal matrices triu upper
triangular part of a matrix tril lower triangular part of a matrix rand
randomly generated matrix
o Make sure you ask for help on all the above commands.
command eye(4,4): identity matrix of size 4
zeros(2,3): zeros creates a matrix of zeros
ones(2): command ones creates a matrix of ones.
rand command: randomly generated matrix,
rand(5,4): 5X4 matrix with entries that will be uniformly distributed between
0 and 1.
The commands triu and tril, extract the upper and lower part of a matrix,
respectively.
diag(D) produces a column vector, whose components are the elements of D
that lie on its main diagonal
summarize some of the commands in the second sub-category of matrix
functions.
o size size of a matrix
o det determinant of a square matrix
o inv inverse of a matrix
o rank rank of a matrix
o rref reduced row echelon form
o eig eigenvalues and eigenvectors
o poly characteristic polynomial
o norm norm of matrix (1-norm, 2-norm, -norm)
o cond condition number in the 2-norm

lu LU factorization
qr QR factorization
chol Cholesky decomposition
svd singular value decomposition
PROGRAMMING IN MATLAB
M-files : Scripts and functions
There are two types of m-files : script files and function files. Script files
contain a sequence of usual MATLAB commands, that are executed (in order)
once the script is called within MATLAB.
o
o
o
o

t = 0:pi/20:2*pi; [x,y] = meshgrid(t);


% look up meshgrid
subplot(2,2,1) % creates a 2x2 array of plots, and plot in the first subplot
plot(sin(t),cos(t)) axis equal % this is a parametric plot
subplot(2,2,2) z = sin(x)+cos(y); % z is a matrix plot(t,z) axis([0 2*pi -2 2])
plotting each column of z % versus t
subplot(2,2,3) z = sin(x).*cos(y); plot(t,z) axis([0 2*pi -1 1])
subplot(2,2,4) z = (sin(x).^2)-(cos(y).^2); plot(t,z); axis([0 2*pi -1 1])

You might also like