Matlab Cheat Sheet
Defining Matrices 

Row vector  x=[1 2 3] 
Column vector  x=[1 2 3]' 
Matrix  A = [1 2; 3 4] or A=[1 2 3 4] 
Transpose  use the apostrophe  B = A' 
Addition  C=A+B 
Multiplication  D = A*B 
Ranges 

By integers from 0 to N  x=0:N 
By steps of .1  x=0:.1:N 
Assign part of a matrix  E(3:4,1:2) = A 
Assign an entire column of a matrix  F(:,1) = x' 
Sizes 

Size of a matrix  [m,n] = size(A) 
Length of a vector  n = length(x) 
Special Functions 

Trig functions  sin(x) cos(x) etc. 
Zero matrix N x N zero matrix M x N zero matrix 
zeros(N) zeros(M,N) 
Identity matrix  eye(N) 
Random matrix N x N random matrix with entries between 0 and 1 M x N random matrix 
rand(N) rand(M,N) 
Plotting 

2D plot with abscissae in x, ordinates in y  plot(x,y) 
2D plot of sin(x)  plot(x,sin(x)) 
2D plot of sin(x) using green diamonds to indicate points  plot(x,sin(x),'gd') 
Saving and Loading 

Save current variables in "matlab.mat"  save 
Save current variables in "myfile.mat"  save myfile 
Load variables from "matlab.mat"  load 
Load variables from "myfile.mat"  load myfile 
Read commadelimited data from "myfile.csv"  dlmread('myfile.csv',',') 
Read spacedelimited data  dlmread('myfile.csv',' ') 
Eigenvalues 

Eigenvalues of a matrix  eig(A) 
Eigenvalues and eigenvectors of A  [evec,eval]=eig(A) 
QR decomposition of a matrix  [Q,R] = qr(A) 
Gauss elimination 

LU decomposition of a matrix  [L,U] = lu(A) 
LU with a permutation matrix  [L,U,P] = lu(A) 
Cholesky decomposition of a symmetric matrix  R = chol(A) 
Solve Ax = b for x  x = A\b 
Assignment 7 is posted.
A solution to the test is
available.