Matlab Cheatsheet for Linear Algebra
Table of Contents
Solve augmented matrix
Use A for the coefficient matrix and B for the augmented column:
>> A=[1 2 -3; 0 1 -3; -3 -5 7]
A =
1 2 -3
0 1 -3
-3 -5 7
>> B=[-2; -5; 3]
B =
-2
-5
3
>> c = linsolve(A,B)
c =
2.0000
1.0000
2.0000
Return eigenvalues D and eigenvectors V
Where D represents \(\Lambda\) and V represents \(S\). Matlab seems to present the eigenvalues in ascending order and S columns will be sorted accordingly.
>> A=[.5 .4; -.104 1.1]
A =
0.5000 0.4000
-0.1040 1.1000
>> [V,D] = eig(A)
V =
-0.9806 -0.6097
-0.1961 -0.7926
D =
0.5800 0
0 1.0200
>>
Scale eigenvectors
- Matlab will provide normalized eigenvectors that have magnitude 1 (sum the squares of all the entries in a column of a 2 by 2 matrix and you get 1). To “normalize” it in a way that the first entry row is 1 we can divide each vector by it’s first element, which is vectorized using
bsxfun(@rdivide, V, V(1,:))
. Hopefully we got rid of long decimals and can use the basic calculator to figure out whole number columns.
Transpose
B = transpose(A)
B = A.'
Vector length
n = norm(v)
Matrix multiplication
C = mtimes(A,B)
- also works with just *
Matrix sum
S = sum(A)
Matlab comments
%{
Comment
block
%}
% comment line
Inverse matrix
S=inv(A)
%or
S=A^(-1)
Identity Matrix
I = eye(n) %returns an n-by-n identity matrix with ones on the main diagonal and zeros elsewhere.
I = eye(n,m) %returns an n-by-m matrix with ones on the main diagonal and zeros elsewhere.
Zero matrix
O = zeros(n) %returns an n-by-n matrix of zeros.
O = zeros(n,m) %returns an n-by-m matrix of zeros.
Row reduced echelon form
R = rref(A) %returns the reduced row echelon form of A using Gauss-Jordan elimination with partial pivoting.
Normalize columns of matrix
N=normc(M) %normalizes the columns of M to a length of 1.