Show Menu
Cheatography

ENG EK125 Matlab Cheat Sheet (DRAFT) by

BU ENG EK 125 Matlab Cheat Sheet.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic

Help
Exit
Constants
Format
Type
ML
doc
quit
pi
compact
cast(b­,'l­ike',a)
A=TP+TN/T
help
exit
i
loose
char('­a'+­1)='b'
P=TP/TP+FP
demo
 
j (=i)
long (15 dp.)
lookfor
 
inf (∞)
short (4 dp.)

Random­izing

Real Numbers
Integers
Seeding
rand: range 0-1
round(­ran­d*N): range 0-N
rng('s­huf­fle'): sets clock as seed
rand*N: range 0-N
randi(n): range 1-n
rng(in­tseed): introduces seed
randi*­(hi­gh-­low­)+low: range low-high
randi(­[mi­n,m­ax]): range min-max
rng('d­efa­ult'): default seed

Numerical

Division
Round
Root
Logs
Degrees
Sign
rem(3,2)=1
fix
sqrt
log(x)=ln
deg2rad
sign(n)=1 if n>0
mod(3,2)=1
floor
nthroo­t(6­4,3)=4
log2(x­)=base 2
rad2deg
sign(n)=0 if n=0
 
ceil
 
log10(­x)=base 10
 
sign(n)=-1 if n<0
 
round
 
exp(n)=en

Arrays

Creating
Referring
linspa­ce(­x,y,n): linear spacing, range x-y, n elem (def n=100)
mat(n): nth element
logspa­ce(­x,y,n): log spacing, range x-y, n elem (def n=50)
length­(vec): total elem in vec
rand(n,m): n*m rand matrix
[r, c]=siz­e(mat): r=rows, c=columns
randi(­[mi­n,m­ax]­,n,m): n*m int matrix
length­(math): r/c length (biggest)
zeros(­n,m): n*m matrix of 0
empty vec: [ ]
ones(n,m): n*m matrix of 1
numel(­mat): total elem in mat

Misc Matrix

diff
diff between consec­utive vec
dot
dot product
any
if true for at least 1 elem
cross
cross product
all
if true for all elem
diag
returns diag of matrix
find
returns indices that meet criteria
trace
sum of elem in diag
isequal
compares arrays
eye
n*n identity matrix
isdiag
true if diag matrix
issymm­etric
true if symmetric
mean(d­ata­,'o­mit­nan')
ommits NaN to calc mean

Plotting

Basic
**Colors
Markers
plot(x­,y,­'r*')

axis([minx maxx miny maxy])
b: blue
.: point
^: up triangle
xlabel, ylabel, title
g: green
o: circle
<: left triangle
bar, grid, scatter, corrcoef
r: red
x: x
>: right triangle
Lines
c: cyan
+: plus
p: pentagram
-: solid
m: magenta
*: star
h: hexagram
: : dotted
y: yellow
s: square
|: vertical line
-.: dash dot
k: black
d: diamond
-: horizontal line
—: dashed
w: white
v: down triangle

Functions

function out = funcna­me(in)

%define for out

end
Basic user defined function, % for comments
function [x,y,z] = fnname­(a,b)

%print, plot, display

end
Calling with only 1 output var gives 1st value only
function fnname­(x,y)

%print, plot, display

end
Input args are not always needed (call
fnname
or
fnname()
)
primary function header

body with sub function call

end

sub function header

body

end
Subfun­ction can only be called by name of the primary
function outs = pers(x)

persistent runsum

if isempt­y(r­unsum)

runsum = 0;

end

runsum­=ru­nsum*x;

outsum­=ru­nsum;

end
Persistent variables
function out = fnname(x)

persistent R

if isempty(R)

R=0;

end

out=R+x;

R=x;

end
Adds to previous
function [x,y] = coor(t­heta)

x=1;

y=2;

end
Function stub

Matrix Manipu­lation

reshap­e(m­at,n,m)
re-dim­ensions columnwise
fliplr
flips matrix left to right
flipud
flips matrix upside down
rot90
rotates matrix counte­rcl­ockwise 90°
repmat­(ma­t,n,m)
replicates n*m copies of mat
repele­m(m­at,n,m)
replicates elements from mat n*m times
cumsum­/cu­mprod
cumulative sum/pr­oduct
min(vec)
minimum vector value
max(vec)
maximum vector value
sum(vec)
sum vector elements
prod(vec)
product vector elements
abs(vec)
absolute vector values