在这篇文章中,我会为Matlab和Octave提供一个简单的参考。Octave是一个设计为提供类似于Matlab的免费工具的GNU程序。我认为Octave和Matlab之间可能没有100%的兼容性,但我注意到大多数基本命令是兼容的。我会尝试列出那些可以在Matlab和Octave中通用的命令。这里列出的所有示例代码都是我用Octave尝试的,而不是Matlab。
虽然有很多功能在这里没有解释,但我会尝试列出那些在大多数你可以找到的Matlab样本脚本中最常用的函数。我的目的是为您提供一组基本命令和示例,这样您就可以至少阅读从这里和互联网上获得的大多数样本脚本(例如,互联网),而不会让您感到压倒性。如果您熟悉这些最小功能集,您将对工具有一定的了解,然后您将理解Mathworks或GNU Octave的官方文档,该文档解释了所有函数,但没有太多示例。
我还没有完成“我认为是最小集合”的东西,我希望我能在几周内完成。请保持关注!
矩阵(二维或更高维度数组)
创建矩阵
通用形式
单位矩阵 - eye()
M x N零矩阵 - zeros()
M x N全一矩阵 - ones()
数学运算
加法
减法
逐元素乘法 - element by element
内积乘法 - inner Product
幂 - element by element
幂 - matrix
除法
转置
行列式
逆矩阵
索引元素(引用元素)
matrixname(rowindex,colindex)
matrixname(:,colindex)
matrixname(rowindex,:)
矩阵特定操作
特征值和特征向量
分解
LU分解
重新排列元素
circshift()
resize()
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