添加链接描述
矩阵(二维或更高维度数组)
创建矩阵
通用形式
单位矩阵 - eye()
M x N 零矩阵 - zeros()
M x N 全一矩阵 - ones()
数学运算
加法
减法
逐元素乘法 - element by element
逐元素乘法 - 内积
幂 - element by element
幂 - matrix
除法
转置
行列式
逆矩阵
索引元素(引用元素)
matrixname(rowindex,colindex)
matrixname(:,colindex)
matrixname(rowindex,:)
矩阵特定操作
特征值和特征向量
分解
LU分解
重新排列元素
循环移位()
调整大小()
< Creating a Matrix >
Method 1 : m = [ value value value ; value value value ; ...]
Ex)
Input
m = [1 2 3; 4 5 6; 7 8 9]
Output
m =
1 2 3
4 5 6
7 8 9
Method 2 : m = [ vector ; vector ; ...]
Ex)
Input
v1 = [1 2 3];
v2 = [4 5 6];
v3 = [7,8,9];
m = [v1; v2; v3]
Output
m =
1 2 3
4 5 6
7 8 9
< Creating an Identity Matrix >
Method 1 : m = eye(M) // M x M identity matrix
Ex)
Input
m = one(3)
Output
m =
1 0 0
0 1 0
0 0 1
< Creating M x N Zero Matrix >
Method 1 : m = zeros(M,N)
Ex)
Input
m = zeros(4,2)
Output
m =
0 0
0 0
0 0
0 0
Method 2 : m = zeros(M) // M x M square matrix
Ex)
Input
m = zeros(3)
Output
m =
0 0 0
0 0 0
0 0 0
< Creating a M x N one matrix >
Method 1 : m = ones(M,N)
Ex)
Input
m = ones(4,2)
Output
m =
1 1
1 1
1 1
1 1
Method 2 : m = ones(M) // M x M square matrix
Ex)
Input
m = one(3)
Output
m =
1 1 1
1 1 1
1 1 1
< Mathematical Operation >
Case 1 : m = matrix1 + matrix 2;
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m2 = [9 8 7; 6 5 4; 3 2 1];
m = m1 + m2
Output
m =
10 10 10
10 10 10
10 10 10
Case 2 : m = matrix1 .* matrix 2; // element by element multiplication
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m2 = [9 8 7; 6 5 4; 3 2 1];
m = m1 .* m2
Output
m =
9 16 21
24 25 24
21 16 9
Case 3 : m = matrix1 * matrix 2; // Inner Product
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m2 = [9 8 7; 6 5 4; 3 2 1];
m = m1 * m2
Output
m =
30 24 18
84 69 54
138 114 90
Case 4 : m = matrix1 .^ n; // power of n for each element
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m2 = [9 8 7; 6 5 4; 3 2 1];
m = m1 .^ 2;
Output
m =
1 4 9
16 25 36
49 64 81
Case 5 : m = matrix1 ^ n; // matrix power
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m2 = [9 8 7; 6 5 4; 3 2 1];
m = m1 ^ 2;
Output
m =
30 36 42
66 81 96
102 126 150
Case 6 : tm = m'; // transpose matrix
Ex)
Input
m = [1 2 3; 6 5 4; 7 3 9];
tm=m'
Output
tm =
1 6 7
2 5 3
3 4 9
Case 7 : im = inv(m); // take the inverse matrix
Ex)
Input
m = [1 2 3; 6 5 4; 7 3 9];
im=inv(m)
Output
im =
-0.47143 0.12857 0.10000
0.37143 0.17143 -0.20000
0.24286 -0.15714 0.10000
Case 8 : im = det(m); // take the determinant of the matrix
Ex)
Input
m = [1 2 3; 6 5 4; 7 3 9];
dm=det(m)
Output
dm =
-70
< Indexing the elements - matrixname(rowindex,colindex) >
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(1,2)
Output
ans = 2
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(1,2) = 0
Output
m =
1 0 3 4
5 6 7 8
9 10 11 12
< Indexing the elements - matrixname(:,colindex) >
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(:,2)
Output
ans =
2
6
10
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(:,2) = 0
Output
m =
1 0 3 4
5 0 7 8
9 0 11 12
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(:,2) = [15;16;17]
Output
m =
1 15 3 4
5 16 7 8
9 17 11 12
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(:,2) = [15 16 17]'
Output
m =
1 15 3 4
5 16 7 8
9 17 11 12
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(:,2) = [15 16 17].'
Output
m =
1 15 3 4
5 16 7 8
9 17 11 12
< Indexing the elements - matrixname(rowindex,:) >
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(2,:)
Output
ans =
5 6 7 8
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(2,:) = 0
Output
m =
1 2 3 4
0 0 0 0
9 10 11 12
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12];
m(2,:) = [15 16 17 18]
Output
m =
1 2 3 4
15 16 17 18
9 10 11 12
< Matrix Operators >
Case 1 : Eigenvalue and Eigenvector
Ex)
Input
A = [1.01 0;0 0.9];
[v,d]=eig(A)
Output
v =
0 1
1 0
d =
Diagonal Matrix
0.90000 0
0 1.01000
Input
A*v
Output
ans =
0.00000 1.01000
0.90000 0.00000
Input
v*d
Output
ans =
0.00000 1.01000
0.90000 0.00000
< Decomposition - LU Decomposition >
Ex)
Input
m = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]
Output
m =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Input
[L,U] = lu(m)
Output
L =
0.07692 1.00000 0.00000 0.00000
0.38462 0.66667 -0.63988 1.00000
0.69231 0.33333 1.00000 0.00000
1.00000 0.00000 0.00000 0.00000
U =
13.00000 14.00000 15.00000 16.00000
0.00000 0.92308 1.84615 2.76923
0.00000 0.00000 -0.00000 -0.00000
0.00000 0.00000 0.00000 0.00000
Input
L * U
Output
ans =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
< Rearranging Elements - circshift() >
Case 1 : m = circshift(m1,N) // Where N is a positive Number. This shift rows
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = circshift(m1,1)
Output
m =
7 8 9
1 2 3
4 5 6
Case 2 : m = circshift(m1,N) // Where N is a Negative Number. This shift rows
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = circshift(m1,-1)
Output
m =
4 5 6
7 8 9
1 2 3
Case 3 : m = circshift(m1,[0 N]) // Where N is a Positive Number. This shift cols
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = circshift(m1,[0 1])
Output
m =
3 1 2
6 4 5
9 7 8
Case 3 : m = circshift(m1,[0 N]) // Where N is a Negative Number. This shift cols
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = circshift(m1,[0 1])
Output
m =
2 3 1
5 6 4
8 9 7
< Rearranging Eelemetns - resize() >
Case 1 : m = resize(m1,M,N,..)
// Resize Matrix m1 to be an M x N x .. matrix, where M, N is smaller than the original matrix
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = resize(m1,3,2)
Output
m =
1 2
4 5
7 8
Case 2 : m = resize(m1,M,N,..)
// Resize Matrix m1 to be an M x N x .. matrix, where M or N is smaller than the original matrix
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = resize(m1,4,5)
Output
m =
1 2 3 0 0
4 5 6 0 0
7 8 9 0 0
0 0 0 0 0
Case 3 : m = resize(m1,M) // Resize Matrix m1 to be an M x M matrix
Ex)
Input
m1 = [1 2 3; 4 5 6; 7 8 9];
m = resize(m1,2)
Output
m =
1 2
4 5