Table of contents
4.1 Definition and construction of matrix
7.1. Two-dimensional plane drawing
The effect of adding ; after the statement is: the operation result of the changed line will not be displayed in the command line window when the program is running.
format compact: % output line spacing is set to compact mode
format loose: % restore to the default loose mode
It should be noted that
format
the setting of the function will only affect the output line spacing in the current MATLAB session, and will not change the actual data or variables. Also, this setting may have no effect on graphics and other non-command line output.
1. Help command
2. Data type
%% 独占一行的注释
% 普通注释
clear all 删除所有变量
clc 删除命令行窗口内容
变量命名规则:区分大小写;长度不超过63
10/3=3.3333 /是真除法
abs('a') %输出97 单引号表示字符
abs('ab') %输出97 98
char(97) %输出'a'
num2str(97) %输出'a'
length('adjkasnjfka') #输出的是字符串长度
A=[1 2 3;4 5 6;7 8 9] %定义矩阵
B=A' %转置
C = A(:) %竖向拉长
D = inv(A) %逆矩阵(必须时方阵才能求逆矩阵)
A * D (相当于A×A的逆)
在MATLAB中,可以使用inv函数或者det函数来判断一个矩阵是否可逆。
·inv函数用于计算矩阵的逆。如果一个矩阵是可逆的,inv函数将返回该矩阵的逆矩阵。如果矩阵不可逆(奇异矩阵),inv函数将引发一个异常。
·det函数用于计算矩阵的行列式。如果一个矩阵的行列式不等于零,则说明该矩阵是可逆的;如果行列式等于零,则说明该矩阵是不可逆的。
E = zeros(10,5,3) 创建一个10行5列3维的全0矩阵
E(:,:,1) = rand(10,5) %rand生成均匀分布的伪随机数。分布在(0~1)之间
rand(m,n,‘double’)生成指定精度的均匀分布的伪随机数,参数还可以是’single’
rand(): Generates a decimal of [0,1)
rand(m): generate a square matrix of m X m, filled with random decimals
rand(m,n): generate a matrix of m X n, filled with random decimals
rand(m,n,p): generate a matrix of m X n X p, filled with random decimals
a+(ba)*rand(): [b,a] interval decimal
'double' and 'single': specify the data type, placed in the last parameter position of rand
randi is the generated integer and rand is a decimal in [0,1].
randi(iMax): generate an integer of [0, iMax]
randi(iMax,m): generate a square matrix of m X m, the value interval [0,iMax]
randi(iMax,m,n): generate a matrix of m X n, the value range [0,iMax]
randi(iMax,m,n,p,...): All the following are dimensions m X n X p X ...
randi([iMin,iMax],m,n): generate a matrix of m X n, the value interval [iMin,iMax]
randn(m):m X m
randn(m,n):m X n
randn(m,n,p):m X n X p
· Only randi can specify [iMin, iMax], neither rand nor randn.
· randi generates integers, and rand and randn generate decimals.
· randn can only specify dimensions.
3. Cell arrays and structures
Cell array: It is a unique data type in MATLAB. It is a kind of array. Its internal elements can belong to different layout types . In terms of conceptual understanding, it can be considered as the structure in C language and the object in C++. very similar. Cell array is a characteristic data type in MATLAB, which is different from other data types (such as character type, character array or string, and general arithmetic data and arrays). Its unique data access method determines its characteristics. It gives people a feeling of querying information, and can gradually track until all variables are translated into basic data information. Its class function output is cell (cell)
Cell array
A = cell(1,6) %Define
A{2} = eye(3) %Matlab subscripts before version 2021 start from 1
A{5} = magic(5) %Magic cube: the matrix is horizontal, The sum of the numbers in the three directions vertically or obliquely is always the same
B = A{5}
structure
Note that the definitions are separated by commas, use dots when accessing.
>> books=struct('name',{ {'Machine Learning','Data Mining'}},'price',[30,40]) books = 包含以下字段的 struct: name: {'Machine Learning' 'Data Mining'} price: [30 40] >> books.name ans = 1×2 cell 数组 'Machine Learning' 'Data Mining' >> books.price ans = 30 40 >> books.name(1) ans = cell 'Machine Learning' >> books.name{1} ans = Machine Learning
4. Matrix operation
4.1 Definition and construction of matrix
>> A=[1,2,3,4,5,6,7,8,9]%直接定义矩阵
A =
1 2 3 4 5 6 7 8 9
>> B=1:2:9 %第二个参数为步长,不可缺省,[1,9]包含最后一个数值
B =
1 3 5 7 9
>> C = repmat(B,3,2) %重复执行3行2列
C =
1 3 5 7 9 1 3 5 7 9
1 3 5 7 9 1 3 5 7 9
1 3 5 7 9 1 3 5 7 9
>> D = ones(2,4) %生成一个2行4列的全1矩阵
D =
1 1 1 1
1 1 1 1
4.2 Four operations of matrix
>> A = [1 2 3 4; 5 6 7 8]
A =
1 2 3 4
5 6 7 8
>> B = [1 1 2 2; 2 2 1 1]
B =
1 1 2 2
2 2 1 1
>> C=A+B %对应位置相加
C =
2 3 5 6
7 8 8 9
>> C=A*B' %'是转置符号
C =
17 13
41 37
>> C=A'*B
C =
11 11 7 7
14 14 10 10
17 17 13 13
20 20 16 16
>> A
A =
1 2 3 4
5 6 7 8
>> B
B =
1 1 2 2
2 2 1 1
>> A.*B %对应位置相乘
ans =
1 2 6 8
10 12 7 8
>> A./B %对应位置相除
ans =
1.0000 2.0000 1.5000 2.0000
2.5000 3.0000 7.0000 8.0000
>> G = A / B %相当于A*B的逆 G*B = A G*B*pinv(B) = A*pinv(B) G = A*pinv(B),相当于A乘B
G =
1.8333 -0.1667
3.1667 1.1667
>> A * inv(B)
错误使用 inv
矩阵必须为方阵。
>> A * pinv(B) %pinv可以不是方阵
ans =
1.8333 -0.1667
3.1667 1.1667
4.3 Subscripts of matrices
>> A=magic(5)
A =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> A(2,3) %矩阵坐标从1开始数,访问第二行第二列元素
ans =
7
>> A(2,:) %访问第二行所有元素
ans =
23 5 7 14 16
>> A(:,3) %访问第三列所有元素
ans =
1
7
13
19
25
>> find(A>20) %返回的是大于20的元素的下标,竖着数
ans =
2
6
15
19
23
>> A(6)
ans =
24
5. Program structure
5.1 for loop structure
>> sum=0;
>> for i=1:5
sum=sum+i*i;
end
>> sum
sum =
55
sum=0;
for i=1:5
cur=1;
for j=1:i
cur=cur*j;
end
sum=sum+cur;
end
disp(sum); %153
%打印九九乘法表
for i=1:9
for j=1:i
a(i,j)=i.*j;
end
end
disp(a);
>> test01
1 0 0 0 0 0 0 0 0
2 4 0 0 0 0 0 0 0
3 6 9 0 0 0 0 0 0
4 8 12 16 0 0 0 0 0
5 10 15 20 25 0 0 0 0
6 12 18 24 30 36 0 0 0
7 14 21 28 35 42 49 0 0
8 16 24 32 40 48 56 64 0
9 18 27 36 45 54 63 72 81
5.2 Branch structure
7. Basic drawing operations
7.1. Two-dimensional plane drawing
x=0:0.01:2*pi;
y=sin(x);
figure; %建立一个幕布
plot(x,y);
title('y=sin(x)');
xlabel('x');
ylabel('y');
xlim([0 2*pi]); %设置x坐标值的范围
x=0:0.01:20;
y1=200*exp(-0.05*x).*sin(x);
y2=0.8*exp(-0.5*x).*sin(10*x);
figure;
[AX,H1,H2]=plotyy(x,y1,x,y2,'plot');%共用一个x的坐标系,在y上有两个不同的取值
%设置相应的标签
set(get(AX(1),'Ylabel'),'String','Slow Decay');
set(get(AX(2),'Ylabel'),'String','Fast Decay');
xlabel('Time(\musec)');
title('Multiple Decay Rates');
set(H1,'LineStyle','--','Color','b');
set(H2,'LineStyle',':','Color','k');
6.2 3D Stereo Drawing
t=0:pi/50:10*pi;
plot3(sin(t),cos(t),t);
xlabel('sin(t)');
ylabel('cos(t)');
zlabel('t');
%hold on
%hold off %不保留当前操作
grid on;%把图片绘制出来,加一些网格线
axis square %使整个图(连同坐标系)呈方块
hold on means that the current axes and images are kept without being refreshed, ready to accept the graphics that will be drawn later, and multiple graphics coexist , that is, the graphics hold function is activated, the current coordinate axes and graphics will be kept, and the graphics drawn from then on will be added to this graphics , and automatically adjust the range of the coordinate axes.
Hold off makes the current axis and image no longer have the property of being refreshed, and when a new image appears, the original image is canceled . That is, turn off the graph hold function.
hold on and hold off are used relatively.
7. Save and export graphics
If the image generated by matlab is directly intercepted by screenshot, the clarity of the image will be affected. Therefore, we suggest that you can use the following methods to save and export graphics.
1) as shown in the figure
2) Edit→Copy option
Adjustable corresponding elements
3) Edit → Figure Properties
4) File → Export Settings
By adjusting the pixel value attributes such as width and height, the text can still be clear even if the picture is small.
This is the end of the basic part of Matlab, let's make a little addition~
8. Supplement
[x,y,z] = peaks(30); %peaks命令用于产生双峰函数或者是用双峰函数绘图
mesh(x,y,z)
grid
>> peaks
z = 3*(1-x).^2.*exp(-(x.^2) - (y+1).^2) ...
- 10*(x/5 - x.^3 - y.^5).*exp(-x.^2-y.^2) ...
- 1/3*exp(-(x+1).^2 - y.^2)