Department of Mathematics

Math 300: Mathematical Computing

Matlab Cheat Sheet

Defining Matrices

Row vector x=[1 2 3]
Column vector x=[1 2 3]'

A = [1 2; 3 4]


A=[1 2

3 4]

Transpose - use the apostrophe B = A'
Addition C=A+B
Multiplication D = A*B


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'


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

Special Functions

Trig functions




Zero matrix

N x N zero matrix

M x N zero matrix




Identity matrix eye(N)

Random matrix

N x N random matrix with entries between 0 and 1

M x N random matrix





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 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 C is posted.

The test solution is available.

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