MATLAB
1. Variable
声明变量并赋值即可存储变量, 默认为ans
>> a = 1 |
2. Matrics and Arrays
2.1 Create Matrix and Array
row vector
>> a = [1 2 3 4]
a =
1 2 3 4multiple rows
>> a = [1 2 3; 4 5 6; 7 8 10]
a =
1 2 3
4 5 6
7 8 10create matrix by function
rand(a, b, c)的参数说明: a表示矩阵行数, b表示矩阵列数, c表示矩阵个数, 矩阵的元素范围为[0, 1]的随机数>> a = zeros(3)
a =
0 0 0
0 0 0
0 0 0
>> b = ones(3)
b =
1 1 1
1 1 1
1 1 1
>> c = rand(3,3,1)
c =
0.7952 0.4456 0.7547
0.1869 0.6463 0.2760
0.4898 0.7094 0.6797
2.2 Matrix and Array Operations
Array
>> a = [1 2 3]
a =
1 2 3
>> a + 10
ans =
11 12 13
>> a * 10
ans =
10 20 30Matrix
>> a = [1 2 ; 3 4]
a =
1 2
3 4
>> a + 10
ans =
11 12
13 14
>> a' %{transpose a matrix}%
ans =
1 3
2 4
>> inv(a) %{inverse a matrix}%
ans =
-2.0000 1.0000
1.5000 -0.5000
>> a * inv(a)
ans =
1.0000 0
0.0000 1.0000
>> a .* a %{element-wise multiplication}%
ans =
1 4
9 16
>> a^2
ans =
7 10
15 22
>> a.^2 %{element-wise power}%
ans =
1 4
9 16
2.3 Concatenation
>> a = [1 2; 3 4] |
3. Complex Numbers
>> sqrt(-1) |
4. format
format函数用于控制输出的显示格式, 默认为short. MATLAB支持的格式有常用的以下几种:
- short: 5位字长定点数
- long: 15位字长定点数
- hex: 16进制
- compact: 压缩空格
- rat: 分数
>> format short
>> a = 1.2
a =
1.2000
>> format long
>> a = 1.2
a =
1.200000000000000
>> format hex
>> a = 1.2
a =
3ff3333333333333
>> format rat
>> a = 1.2
a =
6/5
5. Indexing
magic(n)函数: 生成元素范围为$[1, n^2]$的矩阵, 每一行和每一列的和相同
>> A = magic(3) |
MATLAB中的坐标必须为正整数, 越界报错
>> A(3,1) %{get the specify row and column subscript}% |
也可通过元素的依次顺序获得元素(从左到右, 从上到下排序)
>> A(9) |
colon operator(冒号)可指定矩阵的一块区间
>> A(1:3, 2) %{first three rows and the second column}% |
colon operator前后不标记start和end说明表示整个区间
>> A(2, :) %{all second row}% |
通过start:step:end可生成均匀分布的vector
>> 0:2:10 |
通过赋值空数组来实现删除行和列
>> X = magic(4); |
6. Workspace Variable
6.1 View the contents of workspace
whos语句可查看当前workspace下有多少个variable和其属性. clear命令可清楚当前workspace下的所有variable
>> A = magic(4); |
6.2 Save and Load
save myfile.mat |
7. Text and Character
使用single quote(单引号)来包裹字符串
>> myText = 'Hello, world' |
也可用数组来连接两个或多个text
>> longText = [myText ' - ', 'otherText'] |
数字转字符用num2str或int2str, semicolon用于不显示计算结果
>> f = 71; |
8. Calling Function
max()可返回vector中最大元素值
>> A = [1 2 3]; |
max()也可找出两个matrix中每个位置上最大的元素, 不同行数或不同列数的两个matrix进行max操作时报错
>> A = [1 1 6]; |
max()也可返回最大元素的位置
>> [maxA, loc] = max(A) |
clc命令可清空Command Window中所有输入的指令
9. 2D and 3D Plots
9.1 Line Plots
>> x = 0: pi/100: 2*pi; |
9.2 Different Line
MATLAB支持多种格式和颜色的线段表示, 以下是常用的几种格式:
Specifier | LineStyle |
---|---|
'-' | Solid Line |
'--' | Dashed Line |
':' | Dotted Line |
'-.' | Dashed-dot Line |
Specifier | Color | Specifier | Color |
---|---|---|---|
r | Red | g | Green |
b | Blue | c | Cyan |
m | Magenta | y | Yellow |
k | Black | w | White |
例如:
>> x = 0: pi/100: 2*pi; |
9.3 Add plots to an existing figure
hold on可在不删除之前Plot的情况下添加图像, hold off可删除之前添加的图像
x = 0: pi/100: 2*pi; |
10. Plots
三维的plot由两个变量组成, 函数可表示为 $z = f(x, y)$. 使用meshgrid创建 $(x,y)$ 的集合, 再使用surf展示
>> [x, y] = meshgrid(-2:0.2:2, -2:0.2:2); |
11. Subplots
可使用subplot函数实现在一个window中显示多个plot. subplot(a, b, c)中a表示plot的行数, b表示plot的列数, c表示该plot在window中的位置.
>> t = 0: pi/10: 2*pi; |
12. Programming and Scripts
MATLAB中最简单的程序形式为script, script以**.m**为拓展名, 其中可包含MATLAB命令和函数调用.
12.1 Create a script
edit plotrand % create a script named 'plotrand.m' |
12.2 Run a script
在Editor中写入代码, 之后点击Run即可运行script
n = 50; |
12.3 Loops and Conditional Statements
使用for实现循环, 每一个for循环都需要end来结束循环
nsamples = 5; |
Output:
Iteration #1 |
13. Help and Documentation
Open the function documentation in a separate window
doc mean
View an abbreviated text version of function documentation in the Command Window
help mean
14. Matrices and Magic Squares
14.1 Sum, Transpose and Diag
sum: 求每一列的和
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> sum(A) % compute the sum of each column
ans =
34 34 34 34
>> sum(A, 2) % compute the sum of each row
ans =
34
34
34
34transpose: 共轭转置和非共轭转置
- conjugare transpose($'$)
A'
- nonconjugate transpose($.'$)
A.'
- conjugare transpose($'$)
diag: 求矩阵对角线
>> diag(A)
ans =
16
11
6
1
14.2 Generating Matrices
>> Z = zeros(2,4) |
15. Expressions
15.1 Float
Float 精确度为 16 个十进制数值, 超过16位则进行截断. float的数值范围为 $[10^{-308} - 10^{+308}]$
>> x = 12345678901234567; |
15.2 Integer
Integer有不同的精度: 8bits, 32bits和64bits:
>> x = uint64(12345678901234567); |
15.3. Complex number
Complex number大小的比较根据phase angle来决定:
>> sort([3+4i, 4+3i]) |
15.4 Matrix Operators
Specifier | Arithmetic Operator |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
\ | Left division |
^ | Power |
' | Complex conjugate transpose |
() | Specify evaluation order |
15.5 Array Operator
Specifier | Array Operator |
---|---|
+ | Addition |
- | Subtraction |
.* | Element-by-element multiplication |
./ | Element-by-element division |
.\ | Element-by-element left division |
.^ | Element-by-element power |
.' | Unconjugated array transpose |
15.6 Building Tables
>> n = (0:9)'; |
15.7 Functions
Several special functions privode values of constants
Function | Constant |
---|---|
pi | 3.14159265... |
i | Imaginary unit, -1 |
j | Same as i |
eps | Floating-point relative precision, $\xi = 2^{-52}$ |
realmin | Smallest floating-point number, $2^{-1023}$ |
realmax | Largest floating-point number, $(2-\xi)2^{1023}$ |
Inf | Infinity |
NaN | Not-a-number |
16 Special function
16.1 isfinite()
NaN一般用来表示丢失的数据, 可用isfinite(x)来移除这些数据. isfinite()还可用于移除Inf:
>> x = [2.1 1.7 1.6 1.5 NaN 1.9 1.8 Inf]; |
16.2 std(), mean()
std()用于求standard deviation(标准方差), mean()用于求平均数
>> x = [1 2 3 4 5 6 7 8 9 10] |
16.3 isprime()
isprime()用于判断质数
>> X = magic(4); |
16.4 find()
find()根据条件返回一列索引的向量
>> X = magic(4) |