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 |
The last test will take place at the final exam time on
Tuesday, 12 December, from 1:30-3:30. It will be written as a one-hour (not
50 minute) exam, but you may have the full two hours for it.
In other respects it will be very like the other tests, but
comprehensive - it will emphasize Python, but cover all the
topics we have seen.
There is a
Sample Exam, but be aware that things will have changed somewhat
with the advent of ChatGPT.
Assignment A
is posted.