Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
About
MATLAB
What
is
MATLAB
Commercial
numerical
programming
language,
simula,on
and
visualiza,on
One
million
users
(engineers,
scien,sts,
academics)
MATrix
LABoratory
specializes
in
matrix
opera,ons
Mathworks
-
base
&
add-ons
Open-source
Octave
project
MATLAB
History
Developed
by
Cleve
Moler
(Math/CS
Prof
at
UNM)
in
the
1970s
as
a
higher-level
numerical
programming
language
(vs.
Fortran
LINPACK)
Adopted
by
engineers
for
signal
processing,
control
modeling
Mul,purpose
programming
language
Notes
Todays
focus:
Compare
MATLAB
&
R
for
data
analysis,
contrast
as
programming
languages
MATLAB
is
Base
plus
many
toolboxes
Base
includes:
descrip,ve
stats,
covariance
and
correla,on,
linear
and
nonlinear
regression
Sta,s,cs
toolbox
adds:
dataset
and
category
(like
data.frames
and
factors)
arrays,
more
visualiza,ons,
distribu,ons,
ANOVA,
mul,variate
regression,
hypothesis
tests
->
Interac,ve
programming:
Scripts
and
Read-Evaluate- Print
Loop
Similar
representa,ons
of
data
Both
use
vectors/arrays
as
the
primary
data
structures
Matlab
is
based
on
2-D
matricies;
R
is
based
on
1-D
vectors
Both prefer vectorized func,ons to for loops Variables are declared dynamically
A=[1 2 3; 4 5 6]
Access third element of vector v Access element of matrix A Glue two matrices a1 and a2, same number of rows, side by side Stack two matrices a1 and a2, same number of columns Reshape* matrix A, making it an m x n matrix with elements taken columnwise from A
Operators
Task
Assignment
Whole
Matrix
Opera,ons:
Element-by-element
Opera,ons:
Compute
A-1B
Sums
Logical
operators
(element-by- element
on
vectors/matrices)
=
Multiplication:
A*B
Square
the
matrix:
A^2
Raise
to
power
k:
A^k
A.*B
A./B
A.^k
A\B
Columns
of
matrix:
sum(A)
Rows
of
matrix:
sum(A,2)
a
<
b,
a
>
b,
a
<=
b,
a
>=
b
a
==
b
a
~=
b
AND:
a
&&
b
OR:
a
||
b
XOR:
xor(a,b)
NOT:
~a
<-
or
=
A
%*%
B
A
%*%
A
A
%*%
A
%*%
A
A*B
A/B
A^k
A%*%
solve(B)
colSums(A)
rowSums(A)
a
<
b,
a
>
b,
a
<=
b,
a
>=
b
a
==
b
a
!=
b
AND:
a
&&
b
(short-circuit)
a
&
b
(element-wise)
OR:
a
||
b
a
|
b
XOR:
xor(a,b)
NOT:
!a
avals=2*ones(1,6); v<-c(1,5,3,2,3,7) yvals=6:-1:1; v=[1 5 3 2 3 7]; d<-data.frame(cbind(a=2, d=struct(a, avals, yy=6:1), v) yy, yyvals, fac, v);
If/else statement
if cond command1 command2 else command3 command4 end MATLAB also has the elseif statement.
if (cond) { command1 command2 } else { command3 command4 } R uses chained else if statements. > print(ifelse(c(T,F), 2, 3)) [1] 2 3
ifelse() func,on
Help!
Task
Get
help
on
a
func,on
Search
the
help
for
a
word
Describe
a
variable
Show
variables
in
environment
Underlying
type
of
variable
help
fminsearch
help(pmin)
or
?pmin
??inverse
class(a)
str(a)
ls()
typeof(a)
Func,ons
function
[a,
b]
=
minmax(z)
%
one
function
per
.m
file!
%
assign
to
formal
return
names
a
=
min(z)
b
=
max(z)
end
minmax
<-
function(c,
opt=12)
{
#
functions
are
assigned
to
#
variables
ret
<-
list(min
=
min(z),
max
=
max(z))
ret
#
last
statement
is
#
return
value
}
#
if
minmax
was
created
in
current
#
environment
x
<-
minmax(c(1,
30,
3))
smallest
<-
x$min
Object-Oriented
Programming
Formerly:
objects
were
dened
by
a
directory
tree,
with
one
method
per
le
As
of
2008:
new
classdef
syntax
resembles
other
languages
S3
classes:
anributes
+
syntax
class(object)
plot.lm()
Other
notes
r.matlab
package
Graphics
Matlab
has
much
bener
3-d/interac,ve
graphics
support
R
has
ggplot2
and
much
bener
sta,s,cal
graphics
Addi,onal
Resources
Will
Dwinell,
Data
Mining
in
MATLAB
Computerworld
ar,cle
on
Cleve
Moler
Mathworks
Matlabcentral
Comparison
of
Data
Analysis
packages
( hnp://anyall.org/blog/2009/02/comparison-of-data- analysis-packages-r-matlab-scipy-excel-sas-spss- stata/)
R.matlab
package
stackoverow
Thank You!