Matlab
review
Daegun
Won
Matlab
• Available
at:
h5p://www.cmu.edu/compu<ng/so>ware/all/matlab/download.html
• Need
to
be
on
either
CMU
network
or
VPN
Cisco
AnyConnect
h5p://www.cmu.edu/compu<ng/network/vpn/
• Very
helpful
for
scien<fic
prototyping
Many
built-‐in
func<ons
• Matrix
opera<ons,
plots,
etc.
Libraries
for
signal/image
processing
Basics
• Lines
star<ng
with
%
are
comments
• Use
;
to
suppress
output
• End
lines
with
...
to
wrap
>>
((1+2)*3
-‐
2^2
-‐
1)/2
ans:
2
>>
((1+2)*3
-‐
2^2
-‐
1)/2;
>>
1
+
2
+
3
+
4
+
5
...
+
6
+
7
+
8
+
9
ans:
45
Logics
and
Assignment
• Variable
assignment:
=
• Logical
tests:
>,
<,
>=,
<=,
~=
• Logical
operators:
&,
&&,
|,
||,
…
Making
Arrays
• v
=
[1
2
3
4];
%
row
vector
• v
=
[1;
2;
3;
4];
%
column
vector
• 1:5
%
row
vector
[1,2,3,4,5]
• 1:3:10
%
row
vector
[1,4,7,10]
• Max/min
[val,
ind]
=
max(array)
• sum,
prod,
median,
….
Making
Matrices
(1)
• Which
one(s)
of
the
followings
would
make
a
different
matrix?
m
=
[1
2
3;
4
5
6;
7
8
9]
m
=
[1,2,3;
4,5,6;
7,8,9]
m
=
[[1
2;
4
5;
7
8]
[3;
6;
9]]
m
=
[[1
2
3;
4
5
6];
[7
8
9]]
B
=
[1
2
3;
4
5
6];
C
=
[7
8
9];
A
=
[B;
C];
Making
Matrices(2)
• Crea<ng
all
ones,
zeros,
or
iden<ty
matrices
zeros(rows,
cols),
ones(rows,
cols),
eye(rows,
cols)
• Crea<ng
random
matrices
rand(rows,
cols)
draws
from
Unif[0,
1]
randn(rows,
cols)
draws
from
N(0,
1)
• Gelng
the
size
[rows,
cols]
=
size(matrix);
Accessing
elements
• Individual
elements
• Other
tricks
– A(r,
c)
– A([1,3,5])
%
index
starts
from
1!
– A([1,3],
2:end)
• Accessing
n-‐th
row/ – A(1,
logical([1,0,1]))
column
– A(mod(A,
2)==0)
– A(n,
:)
or
A(:,
n)
– A(:)’
• Access
in
column-‐major
order
• sum(A)?
Accessing
elements
• X(X>0)
=
-‐X(X>0)
• X
=
[3
2
0
4
5];
Y
=
[1
1
1
1
1];
q
=
zeros(1,
length(Y));
q(X~=0)
=
Y(X~=0)
./
X(X~=0);
Matrix
math
• Transpose:
A’
• Inverse:
inv(a)
• Matrix
mul<plica<on
vs.
Element
by
Element
mul<plica<on
A
*
A
A
.*
A
• Same
for
divisions
(/
vs
./)
Func<ons
• Operates
in
a
separate
workspace
• Func<on
name
=
filename
• func<on
return_value
=
func-on_name
(params)
my_func.m
func<on
[y,x]
=
my_func(x)
y
=
x^2;
x
=
x+3;
end;
Scripts
vs
Func<ons
my_script.m
my_func.m
y
=
x^2;
func<on
[y,x]
=
my_func(x)
x
=
x+3;
y
=
x^2;
x
=
x+3;
end;
>>
x
=
2;
my_script;
>>
x
=
2;
[y,
xp]
=
my_func(x);
>>
x
>>
x
ans:
5
ans:
2
>>
y
>>
y
ans:
4
ans:
4
>>
xp
ans:
5
Anonymous
func<ons
>>
c
=
4;
>>
f
=
@(x)
x
+
c;
>>
f(3)
ans:
7
>>
c
=
5;
>>
f(3)
ans:
7
• One
use
case:
f
=
@(x)
x.^2
+
3*x
+
1;
quad(
f,
0,
1)
%
integral
of
f
from
x=0
to
1
Cells
• Like
arrays,
but
can
have
different
types
x
=
{‘hello’,
2,
3};
x{1}
x{2}
x{5}
=
@(x)
x
+
1
x{5}(2)
Plolng
x
=
0
:
pi/20
:
4*pi;
axis
on
/
off
plot(x,
sin(x))
grid
on
/
off
box
on
/
off
whitebg(gcf,
[0,
0,
0])
clf
clf
reset
xlabel(‘Angle
\theta’)
ylabel(‘y
=
sin(\theta)’)
<tle(‘The
Sine
Func<on’)
Mul<ple
plots?
clf
hold
on
plot(x,
sin(x))
• Colors:
r,
g,
b,
w,
c,
m,
y,
k
plot(x,
cos(x),
‘m’)
• Symbols:
.
o
x
+
*
s(quare)
plot(x,
x,
‘go’)
d(iamond)
etc.
• Line
type:
-‐
(solid),
-‐-‐
(dashed),
:
(do5ed),
-‐.
legend(‘sin’,
‘cos’,
‘x’)
(dash-‐dot)
axis([0,
2*pi,
-‐1,
1])
• [xmin,
xmax,
ymin,
ymax]
Other
commands
• List
names
of
variables
in
the
environment
whos
• Clear
the
environment
clear
• Edit
func<ons
and
scripts
edit
<filename>
ONE
COMMAND
YOU
SHOULD
NEVER
FORGET
help
Other
things
to
know
• Useful
operators
>,
<,
>=,
<=,
==,
&,
|,
&&,
||,
+,
-‐,
/,
*,
^,
…,
./,
‘,
.*,
.^,
\
• Useful
Func<ons
sum,
mean,
var,
not,
min,
max,
find,
exists,
clear,
clc,
pause,
exp,
sqrt,
sin,
cos,
reshape,
sort,
sortrows,
length,
size,
length,
setdiff,
ismember,
isempty,
intersect,
plot,
hist,
<tle,
xlabel,
ylabel,
legend,
rand,
randn,
zeros,
ones,
eye,
inv,
diag,
ind2sub,
sub2ind,
find,
logical,
repmat,
num2str,
disp,
...
Extra
materials
• Prof.
Touretzky’s
tutorial:
h5p://www.cs.cmu.edu/~dst/Tutorials/Matlab/day1.pdf
h5p://www.cs.cmu.edu/~dst/Tutorials/Matlab/day2.pdf
h5p://www.cs.cmu.edu/~dst/Tutorials/Matlab/day3.pdf
• MIT’s
Matlab
tutorial
h5p://www.cs.cmu.edu/~tom/10601_fall2012/recita<ons/
MATLAB_tutorial.html