MATLAB Quick Reference: Operators
MATLAB Quick Reference: Operators
MATLAB Quick Reference: Operators
Operators
Matrix Operations Array or Element by Element
+ Addition
- Subtraction
' Transpose matrix (conjugate if complex) .' Transpose matrix (no complex
A' conjugation) A.'
== Equal
~= Not Equal
pi π i −1
inf ∞ j −1
Indexing
Vector Indexing
variable(V) V is a vector of indexes
Examples
A(3) The third element in vector A.
A([3 8 11]) The third, eighth and eleventh elements of A.
A(3:11) The third to eleventh elements of A.
A(11:end) All elements from the eleventh to the last element
Matrix Indexing
variable(R,C) R is a vector of rows and C a vector of Columns
Examples
M(2,3) The element in the second row and third column of M.
M(2, [3 8 11]) Second row, third, eighth and eleventh column.
M(4,3:11) Forth row, third to eleventh column.
M(11:end,2) Eleventh to the last row, second column.
M(5,:) Fifth row, all columns.
M(11:end,:) Eleventh to the last row, all columns.
2
Generating Vectors
<start> : <end> x=1:5 generates x = [ 1 2 3 4 5]
<start> : <separation> : <end> y = 0:5:20 generates y = [ 0 5 10 15 20]
linspace(start,end,n) n = number of elements linspace(0,10,5) generates [ 0 2.5 5 7.5 10]
logspace(d1,d2,n) n elements logarithmically logspace(-1,2,4) generates [0.1 1 10 100]
spaced between 10 d 1 and 10 d 2 .
Utility Matrices
zeros(n) n by n matrix where each element is zero.
zeros(m,n) m by n matrix where each element is zero.
zeros(a,b,c) 3 dimensional array, a by b by c.
ones(m,n) m by n matrix where each element is one.
rand(m,n) m by n matrix of random numbers.
eye(n) n by n identity matrix.
Variable Control
who List all variables in memory.
whos Same as above but with more information.
clear Remove all variables from memory.
clear <variable> Remove specified variables from memory.
Help
help Display help topics.
help <function> Help on a particular function.
lookfor <word> Look for word in function descriptions.
doc <function> Full documentation on function.
3
In Built Functions
Hit fx icon next to prompt for function browser. Only selected functions shown here.
abs(x) The absolute value. Modulus round(x) Round to the nearest integer.
sqrt(x) The square root. ceil(x) Round up.
x
exp(x) The exponential base e. e floor(x) Round down.
log(x) The natural logarithm. log e x fix(x) Round towards zero.
log10(x) The log base 10. log 10 x rem(x,b) Remainder of x divided by b.
Trigonometry
sin(x) Sine. x in radians.
sind(x) Sine. x in degrees.
asin(x) The arcsine. The inverse of sin(x). Radians
asind(x) The arcsine. Degrees
sinh(x) Hyperbolic Sine.
asinh(x) The inverse Hyperbolic Sine.
The above variations are also available for the following functions.
cos(x) Cosine
tan(x) Tangent cot(x) Cotangent
sec(x) Secant csc(x) Cosecant
Complex Numbers
real(z) The real part of z. imag(z) The imaginary part of z.
abs(z) The modulus of z. angle(z) The phase angle of z.
conj(z) The complex conjugate of z.
Matrix
det(A) Determinant sqrtm(A) The matrix square root.
norm(A) Norm expm(A) The matrix exponential base e.
inv(A) Inverse logm(A) The matrix natural logarithm.
d = Eigenvalues,
[v,d]=eig(A)
v = Eigenvectors
Statistics
max(x) Maximum median(x) Median var(x) Variance
min(x) Minimum mean(x) Average std(x) Standard Deviation
4
Polynomials
p = [1 2 3 4 5]; can represent the polynomial x 42x 33x 24x5
y = polyval(p,x) Evaluate polynomial for each value in x.
roots(p) Roots of polynomial.
p = poly( <roots>) Polynomial with given roots.
p = polyfit(x,y,n) Best fit of x,y data points to nth order polynomial.
fprintf Formatting
%f Fixed point %5f 5 characters wide \n New line
%e Exponential Notation %5.2f 2 decimal places (dp) \t Horizontal tab
%g auto chooses %f or %e %-5.2f Left justify \\ Back slash
%s String of Characters %+5.2f Print sign (+ or -) %% Percent character
%d Integer (Whole numbers only)
5
Graph Commands
plot(y) Plot y against index number.
plot(x,y) Plot y against x
plot(x1,y1,x2,y2) Plot y1 against x1 and y2 against x2.
plot(x,y,'r+') Plot y against x using red plus signs.
plot(x1,y1,'r+',x2,y2,'go') Red plus signs for x1 and y1, Green circles for x2 y2.
axis([minX maxX minY maxY]) Set the limits of the graph in X and Y. (Note 4 element vector)
h = figure New graphics window.
figure(h) Change to plotting in figure h.
delete(h) Delete figure h.
clf Clear current figure.
drawnow Force the graph to update now.
6
Programming
Enumerated Loops (for)
The general form of a for loop is :-
Examples
A = rand(1,5); for k = [ 1 7 3 pi i]
for a = A disp(k)
disp(a) end
end
while(<condition>)
<statement>
<statement>
etc
end False
condition
Example
True
A = 7; %Find the square root of A.
x=1; %First guess statement
err=1; %Set error to get started
disp(x);
7
Conditional Execution (if)
General form of a simple if statement.
If <condition>
<statement>
<statement> False
etc condition
end
True
statement
statement
if <condition>
<statement1> False
<statement2> condition
etc
else True
<statement3> statement1 statement3
<statement4>
etc
end statement2 statement4
8
Switch
Execution depends on the value of a variable.
switch <variable>
case 1
<statement1> A True
== statement1
etc 1
case 2
<statement2> False
etc
A True
case 3 == statement2
<statement3> 2
etc
False
otherwise
<statement4> A
etc True
== statement3
end 3
False
The case values can be any value that the switch
statement4
variable can take. You can also put multiple
values after the case.
case {1,2,3,4,5}
<statement>
etc
Functions
sum = a + b;
diff = a - b; [s,d] = add_sub(<expression>,<expression>)
9
Unix Commands
Linux and MAC OSX operating systems are both based on UNIX.
File and Directory Paths
/var/tmp Absolute path from root
p5computing/exercise1 Relative path from current working directory.
. The current directory cp /tmp/myfile .
cd ../exercise2
.. The directory above.
cd ../../..
cp /tmp/myfile ~
~ Your home directory
cd ~/p5Computing
Commands
ls List the contents of the current directory.
ls -a List current directory, showing hidden files.
ls -l List current directory, long format. More information.
ls <directory path> List the contents of the specified directory.
ls -al <directory path> As above showing hidden files and long format.
mkdir <directory name> Make a new directory with the given name.
cd <directory path> Change the current working directory.
pwd Print the current working directory.
cp <file path> <new file name> Copy a file to a new file.
cp <file path> <directory path> Copy a file into the specified directory.
mv <file path> <new file name> Change the name of a file to a new file name.
mv <file path> <directory path> Move a file into the specified directory.
rm <list of files> Remove all files in the list.
rm -i <list of files> Remove all files in the list, asking for confirmation.
rm -R <directory path> Remove a directory and its contents.
rmdir <directory path> Remove an empty directory.
cat <file path> Type file to screen.
more <file path> Type file to screen a page at a time.
man <command> Display manual pages for the command.
Wild Cards
? A single character rm prog?.m Remove prog1.m, prog2.m etc
* A character string cp *.m MatlabFiles Copy all files ending .m to a directory.
10
DOS Commands
The commands you can use in a windows command prompt.
Commands
dir List the contents of the current directory.
dir /w List current directory using wide format.
dir <directory path> List the contents of the specified directory.
mkdir <directory name> Make a new directory with the given name.
cd <directory path> Change the current working directory.
copy <file path> <new file name> Copy a file to a new file.
ren <file path> <new file name> Rename a file to a new file name.
move <file path> <directory path> Move a file into the specified directory.
del <list of files> Delete all files in the list.
del <list of files> /P Delete all files in the list, asking for confirmation.
rmdir <directory path> Remove an empty directory.
type <file path> Type file to screen.
help <command> Display help on command.
Wild Cards
? A single character del prog?.m Remove prog1.m, prog2.m etc
* A character string move *.xls mydir Copy all files ending .xls to a directory.
11
Matlab Quick Reference, Version 2.1
Eric Peasley, Department of Engineering Science, University of Oxford
12