Department of Mathematics

Math 300: Mathematical Computing

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

 
2-D plot with abscissae in x, ordinates in y plot(x,y)
2-D plot of sin(x) plot(x,sin(x))
2-D 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 comma-delimited data from "myfile.csv" dlmread('myfile.csv',',')
Read space-delimited 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 exam has been posted.



Department of Mathematics, PO Box 643113, Neill Hall 103, Washington State University, Pullman WA 99164-3113, 509-335-3926, Contact Us
Copyright © 1996-2015 Kevin Cooper