文章目录
- 前言
- 第一部分
- 一、MATLAB as A Calculator
- 二、Elementary Math Functions 基本数学函数
- 三、Embedding Functions 嵌入函数
- 四、Variables 变量
- 五、Numeric Variable (Data) Type 数值变量(数据)类型
- 六、Special Variables and Constants 特殊变量和常数
- 七、MATLAB Calling Priority 命名优先级
- 八、Command Line Terminal 命令行终端
- 九、Some Useful Functions
- 第二部分
- 一、Array (Vector and Matrix)数组(向量和矩阵)
- 二、Array Indexing 数组索引
- 三、Replacing Entries
- 四、Colon Operator 冒号运算符
- 五、Indexing Using Colon Operator 使用冒号运算符进行索引
- 六、Array Concatenation 数组连接
- 七、Array Manipulation 数组操作
- 八、Some Special Matrix
- 九、Some Matrix Related Functions
- 总结
前言
b站课程《MATLAB教程_台大郭彦甫(14课)》学习记录
第一部分
一、MATLAB as A Calculator
1. Operators: + - * / ^
直接输入到命令行窗口,按ENTER直接算出结果
2. Result is computed, and displayed as ans
3. Precedence rules 优先规则
-
Left-to-right within a precedence group
-
Precedence groups are (highest first):
- Parenthesis ()
- Power (^)
- Multiplication and division (*, /)
- Addition and subtraction (+, -)
二、Elementary Math Functions 基本数学函数
- Function list:
http://www.mathworks.com/help/matlab/functionlist.html
Arithmetic 算术
Trigonometry 三角函数
Exponents and Logarithms 指数和对数
Complex Nembers 复数
Cartesian Coordinate System Conversion 笛卡尔坐标系转换
三、Embedding Functions 嵌入函数
- Functions may be embedded into other functions
- Many lines of code can be condensed into one single command
许多行代码可以压缩成一个命令
四、Variables 变量
-
Variables do NOT need to be declared before assignment
-
A single “equal” sign (=) is the assignment operator:
>> LHS = RHS >> A = 10
-
Upper case/lower case make difference? Yes
-
Can variable names can begin with a number? No
五、Numeric Variable (Data) Type 数值变量(数据)类型
可以在工作区直接双击变量名称查看变量的类型
或者在命令行窗口输入who & whos
>>who
您的变量为:
ans
>> ans
ans =
-0.8415
>> whos
Name Size Bytes Class Attributes
ans 1x1 8 double
六、Special Variables and Constants 特殊变量和常数
-
ans
-
i , j : complex number
-
Inf : ∞
-
eps : 2.2204e-016(数字很小很小)
-
NaN : not a number
-
pi : Π
To list keywords:
>>iskeyword
>> 1/0
ans =
Inf
>> log(0)
ans =
-Inf
>> inf/inf
ans =
NaN
七、MATLAB Calling Priority 命名优先级
cos = 'This string'
cos =
'This string'
>> cos(8)
ans =
'r'
cos 从三角函数变成了一个字符串
1. 把variable从工作区消除:clear + variable
2. clear 后面什么都不加的话是清除所有工作区的variable
3. Numeric Display “Format” 数字显示格式
>> pi
ans =
3.1416
>> format long
>> pi
ans =
3.141592653589793
>> format longE
>> pi
ans =
3.141592653589793e+00
>> 3/13
ans =
2.307692307692308e-01
>> format rat
>> 3/13
ans =
3/13
>> format long
>> 3/13+4/14+5/15
ans =
0.849816849816850
>> format rat
>> 3/13+4/14+5/15
ans =
232/273
八、Command Line Terminal 命令行终端
Observe the difference between
>> a = 10
a =
10
>> b = 10;
>> (不显示运算结果)
【;】 at the end of a command suppresses output to the terminal 在命令的末尾,抑制输出到终端
【(键盘上的)⬆】 display previous commands 显示之前的命令
九、Some Useful Functions
- clc : clear command window display
- clear: remove all variables in the workspace
- who: variables in the workspace
- whos: variable information of the workspace
第二部分
一、Array (Vector and Matrix)数组(向量和矩阵)
Row vector:(行向量)
>> a = [1 2 3 4]
Column vector: (列向量)
>> b = [1; 2; 3; 4]
>> a*b inner product 内积
ans =
30
>> b*a outer product 外积
ans =
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
A = [1 21 6;5 17 9;31 2 7]
A =
1 21 6
5 17 9
31 2 7
二、Array Indexing 数组索引
Select a certain subset of elements inside a matrix
>> A(8) (以矩阵的每个列向量开始标号)
ans =
9
>> A([1 3 5]) (找出序号为1 3 5的数)
ans =
1 31 17
>> A([1 3;1 3]) 变成 序号是1 3 和1 3 的矩阵
ans =
1 31
1 31
>> A(3,2) 找出三行二列的数字
ans =
2
>> A([1 3], [1 3]) 前面是row 后面是column
第一个row和第三个row圈起来;第一个column和第三个column圈起来,得到的交集
ans =
1 6
31 7
三、Replacing Entries
Change the following elements in the matrix:
>> A = [1 21 6;5 17 9;31 2 7]
A =
1 21 6
5 17 9
31 2 7
>> A([4 6])=[76 0]
A =
1 76 6
5 17 9
31 0 7
OR
A([1 3],2)=[76;0]
A =
1 76 6
5 17 9
31 0 7
>> A([1 2], [2 3])=[0 0;0 0]
A =
1 0 0
5 0 0
31 0 7
四、Colon Operator 冒号运算符
Want to create a long array: A = [1 2 3 … 100]
Creates vectors or arrays, and specify for iterations 创建向量或数组,并指定迭代
Syntax: 句法规则
>> B = 1:5
B =
1 2 3 4 5
>> B = 1:2:5
B =
1 3 5
>> B = [1:5; 2:3:15; -2:0.5:0]
B =
1 2 3 4 5
2 5 8 11 14
-2 -3/2 -1 -1/2 0
>> str = 'a':2:'z'
str =
'acegikmoqsuwy'
五、Indexing Using Colon Operator 使用冒号运算符进行索引
How do we delete a row or a column of A?
>> A([1 2], [2 3])=[0 0;0 0]
A =
1 0 0
5 0 0
31 0 7
The expression
A( ,:)=[ ]
delete rows or columns of A
>> A(3,:) = [ ]
A =
1 0 0
5 0 0
六、Array Concatenation 数组连接
Arrays can be formed through concatenation as long as the rectangular shape is preserved
只要保持矩形形状,就可以通过串联形成数组
>> A = [1 2; 3 4]
A =
1 2
3 4
>> B = [9 9; 9 9]
B =
9 9
9 9
>> F = [A B]
F =
1 2 9 9
3 4 9 9
>> E = [A;B]
E =
1 2
3 4
9 9
9 9
>> A = [1 2; 3 4];
>> B = [9 9; 9 9];
>> C = [5 6 7 8];
>> D = [-2 -1 0 1];
>> F = [A B ; C; D]
F =
1 2 9 9
3 4 9 9
5 6 7 8
-2 -1 0 1
七、Array Manipulation 数组操作
Operators on array: + - * / ^ . ‘
- 【.*】对应位置元素相乘
- 【A/B】≈A*inv(B)
- 【./】对应位置元素相除
- 矩阵与实数相加,每个元素上面都加上这个实数
- A/实数=A./实数
- A.^实数 为A中每个实数都变成实数次方
- 【’】转置
A = [1 2 3;4 5 4;9 8 7];
B = [3 3 3;2 4 9;1 3 1];
a = 2;
x1 = A + a
x2 = A / a
x3 = A ./ a
x4 = A ^ a
x5 = A .^ a
C = A’
y1 = A + B
y2 = A * B
y3 = A .* B
y4 = A / B
y5 = A ./ B
x1 =
3 4 5
6 7 6
11 10 9
x2 =
1/2 1 3/2
2 5/2 2
9/2 4 7/2
x3 =
1/2 1 3/2
2 5/2 2
9/2 4 7/2
x4 =
36 36 32
60 65 60
104 114 108
x5 =
1 4 9
16 25 16
81 64 49
C =
1 4 9
2 5 8
3 4 7
y1 =
4 5 6
6 9 13
10 11 8
y2 =
10 20 24
26 44 61
50 80 106
y3 =
3 6 9
8 20 36
9 24 7
y4 =
1/14 2/7 3/14
7/6 0 1/2
137/42 -2/7 -3/14
y5 =
1/3 2/3 1
2 5/4 4/9
9 8/3 7
八、Some Special Matrix
linspace(): linearly spaced vectors 线性的向量
eye(n): n×n identity matrix n×n 单位矩阵
zeros(n1:n2): n1×n2 zero matrix n1×n2 零矩阵
ones(n1,n2): n1×n2 matrix with every entry as 1 n1×n2矩阵,每个条目为1
diag(): diagonal matrix 对角矩阵
rand(): uniformly distributed random numbers 均匀分布随机数
九、Some Matrix Related Functions
max(A) 找出每列的最大的元素
max(max(A)) 每列中最大的元素中最大的元素,即矩阵中最大的元素
sum(A)每一列所有元素的加和
mean(A)每一列元素的平均值
sort(A)对每一列进行从小到大的排序
sortrows(A)对第一列元素进行排序,带着行一起变化
size(A) 判断A的Dimension多大,几个Row,几个Column
length(A) 判断vector 向量的只有一个长度
length是求某一矩阵所有维的最大长度
find(A) 找到第几个等于某个数字的元素位置
>> A = [1 2 3; 0 5 6; 7 0 9]
A =
1 2 3
0 5 6
7 0 9
>> max(A)
ans =
7 5 9
>> max(max(A))
ans =
9
>> min(A)
ans =
0 0 3
>> sum(A)
ans =
8 7 18
>> mean(A)
ans =
8/3 7/3 6
>> sort(A)
ans =
0 0 3
1 2 6
7 5 9
>> sortrows(A)
ans =
0 5 6
1 2 3
7 0 9
>> size(A)
ans =
3 3
>> length(A)
ans =
3
>> find(A == 5)
ans =
5
>> find(A == 6)
ans =
8
END
总结
继续加油吧