MATLAB Final Review#
1 Matrix Input Methods#
>>a=[1 2 3;4 5 6; 7 8 9]
>>a=[1,2,3;4,5,6; 7,8,9]
>>a=[1:3;4:6;7:9]
>> a=[1 2 3
4 5 6
7 8 9]
a =
1 2 3
4 5 6
7 8 9
>>
2 Matrix Multiplication#
A * B
A(m x n) B(p x q) can only be multiplied when n=p, matrix multiplication in linear algebra
A .* B
Same size, element-wise multiplication
3 Matrix Transpose#
Real Matrix Transpose#
A'
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> A'
ans =
1 4 7
2 5 8
3 6 9
Complex Matrix Transpose#
A'
Conjugate transpose
A.'
Non-conjugate transpose
>> A = [1+i 2+i 3+i; 4+i 5+i 6+i; 7+i 8+i 9+i]
A =
1.0000 + 1.0000i 2.0000 + 1.0000i 3.0000 + 1.0000i
4.0000 + 1.0000i 5.0000 + 1.0000i 6.0000 + 1.0000i
7.0000 + 1.0000i 8.0000 + 1.0000i 9.0000 + 1.0000i
>> A'
ans =
1.0000 - 1.0000i 4.0000 - 1.0000i 7.0000 - 1.0000i
2.0000 - 1.0000i 5.0000 - 1.0000i 8.0000 - 1.0000i
3.0000 - 1.0000i 6.0000 - 1.0000i 9.0000 - 1.0000i
>> A.'
ans =
1.0000 + 1.0000i 4.0000 + 1.0000i 7.0000 + 1.0000i
2.0000 + 1.0000i 5.0000 + 1.0000i 8.0000 + 1.0000i
3.0000 + 1.0000i 6.0000 + 1.0000i 9.0000 + 1.0000i
4 Random Distribution#
Uniform Distribution#
>> rand(5) Generates a uniform distribution between [0,1]
ans =
0.8869 0.0768 0.3996 0.6761 0.7636
0.0021 0.7990 0.6387 0.1379 0.8417
0.7377 0.4168 0.6586 0.1675 0.7409
0.4085 0.2020 0.3824 0.3339 0.2610
0.5768 0.7958 0.4104 0.1429 0.9621
Normal Distribution#
>> randn(5) Generates a standard normal distribution N[0,1]
ans =
-0.3534 -2.1492 0.2811 -0.2420 0.1620
-0.5709 -0.5303 1.1760 0.5279 -0.0394
0.8749 0.2686 0.8725 0.5440 0.2960
-1.0153 0.2161 -0.9298 1.5400 -1.1293
0.7904 0.7901 -0.1329 -0.5585 -1.0672
Normal distribution with mean 3 and variance 9
x~N(μ,σ²)
ax+b~N(aμ+b,(aσ)²)
aμ+b = mean
(aσ)² = variance
a*randn(p,q)+b
p,q size of the matrix is p*q
>> 3*randn(2,3)+3
ans =
6.5279 5.1196 4.8656
5.5406 -0.0497 5.2151
5 Determinant#
det()
6 Matrix Inverse#
inv
for square matrices and non-singular
pinv()
for singular matrices, returns the pseudo-inverse
7 Matrix Storage Method#
Matrices are stored column-wise
8 Matrix Summation#
Matrix summation is also done column-wise
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> sum(A)
ans =
12 15 18
9 Matrix Reduction#
>> a = [1:4;5:8;9:12;13:16]
a =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> b = a(2:3,3:4) Rows 2 and 3, Columns 3 and 4
b =
7 8
11 12
Deleting rows or columns
a =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> a(2,:)=[] Delete row
a =
1 2 3 4
9 10 11 12
13 14 15 16
>> a(:,2)=[] Delete column
a =
1 3 4
9 11 12
13 15 16
10 AND, OR, NOT Operations#
Note that non-zero is considered as 1, logical operations are performed afterwards
11 Generating Matrices using diag
Function#
Example: Generate the following matrices using diag
and other functions:
a = [0 0 8; 0 -7 5; 2 3 0]
b = [2 0 4; 0 5 0; 7 0 8]
Then use reshape
function to transform them into row vectors
Functions used:
Functions used:
d=diag(A), if A is a matrix, d is a vector of the diagonal elements of A, if A is a vector, d is a diagonal matrix with A as the diagonal elements
B = fliplr(A), flips the columns of matrix A in the left-right direction
If A is a row vector, fliplr(A) flips the order of the elements in A.
If A is a column vector, fliplr(A) is the same as A.
filpup() flips the matrix upside down
B = reshape(A,...,[],...), specifies a certain dimension, and uses the placeholder [] for the remaining dimensions, so that the product of the dimensions is equal to prod(size(A)).
>> A=diag([8 -7 2]) %Generate a matrix with elements on the diagonal
A =
8 0 0
0 -7 0
0 0 2
>> B=A+diag([5 3],-1) %Generate a matrix with subdiagonal elements and add it to A
B =
8 0 0
5 -7 0
0 3 2
>> a=fliplr(B) %Flip the matrix
a =
0 0 8
0 -7 5
2 3 0
>> A=reshape(a,1,9) %Reshape the matrix into a row vector, 'a.' transposes a
A =
0 0 2 0 -7 3 8 5 0
>> q = diag([2 0 8]) % Main diagonal elements
q =
2 0 0
0 0 0
0 0 8
>> r = diag([4 5 7]) % Subdiagonal elements
r =
4 0 0
0 5 0
0 0 7
>> w = fliplr(r) % Flip the subdiagonal elements left-right
w =
0 0 4
0 5 0
7 0 0
>> b = q+w % Add the two matrices
b =
2 0 4
0 5 0
7 0 8
>> B=reshape(b.',1,[]) % Convert it into a row vector
B =
2 0 4 0 5 0 7 0 8
12 Variable Naming Requirements#
In MATLAB, variable names can only consist of numbers, letters, and underscores, and must start with a letter
. Variable names are also case-sensitive in MATLAB.
13 .m Files#
.m files can be divided into script files and function files. Script files and function files need to be in the same folder to call each other.
Script Files#
Function Files#
Function file format
function [] = function_name()
end
14 Curve Plotting#
plot#
plot(x,y)
plot(x1,y1,'op1',x2,y2,'op2',...,xn,yn) plots multiple graphs on the same plane
y1=2x+1
x = 1:0.1:10
y1 = 2.*x+1
y2=...
plot(x1,y1,...,x2,y2,...)
op Line style Color Marker
Line style
- Solid
: Dotted
-. Dash-dot
-- Dashed
Marker
+
*
.
.
x
s
d Diamond
v
^
<
>
P Pentagon
h Hexagon
Color
r Red
g Green
b Blue
k Black
y Yellow
w White
m Magenta
c Cyan
plot3#
pot3(x1,y1,z1,'op1',x2,y2,z2,'op2',...,xn,yn,zn,'opn') plots 3D graphs
fplot#
fplot('fun',[a,b]) % plots the graph of the function fun in the interval [a,b]
fplot(@(x) 2*exp(-0.5*x)*sin(2*pi*x),[0,2*pi],'gd-')
15 Surface Plotting#
Generating a Meshgrid#
meshgrid(x,y)
x = a:step:b;
y = c:step:d;
[x,y] = meshgrid(x,y);
x = [1 2 3 4]
y = [2 4 6]
[x y] = meshgrid(x,y);
Plotting Mesh Lines#
mesh(x,y,z)
Plotting Filled Surfaces#
surf(x,y,z)
Plotting Contour Lines on a Plane#
contour(x,y,z)
Plotting Contour Lines in 3D#
contour3(x,y,z)
Displaying Multiple Graphs#
subplot(grid_columns,grid_rows,position)
subplot(2,2,3)%A grid with 2 columns and 2 rows, display in position 3, second row first column
Adding a Title#
title("Title")
Adding X-Axis Label#
xlabel("x-axis label")
Adding Y-Axis Label#
ylabel("y-axis label")
Adjusting the Axis Range#
axis(Xmin,Xmax,Ymin,Ymax)
Array Pre-allocation#
y=zeros(size(x))