失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > MATLAB 常用函数学习笔记

MATLAB 常用函数学习笔记

时间:2022-01-09 09:50:12

相关推荐

MATLAB 常用函数学习笔记

持续更新中……

基础功能

clc:clear command window displayclear:remove all variables in the workspacewho:variables in the workspacewhos:variable information of the workspace

矩阵索引 Array Indexing

A=[121651793127]A = \begin{bmatrix} 1 & 21 & 6 \\ 5 & 17 & 9 \\ 31 & 2 & 7 \end{bmatrix}A=⎣⎡​1531​21172​697​⎦⎤​

A(8)% 9A([1 3 5]) % 1 31 17A([1 3; 1 3])% [1 31; 1 31]A(3, 2)% 2A([1 3], [1 3])% 表示第一行和第三行的第一列和第三列[1 6; 31 7]

Some Matrix Related Functions

max(A)% 每一列最大值max(max(A))min(A)% 每一列最小值sum(A)% 每一列求和mean(A)% 每一列求平均sort(A)% 每一列从小到大排序sortrows(A)% 按照第一列 将整行从小到大排序size(A)% 矩阵维度 m nlength(A)% 矩阵长度find(A)% 返回索引

实现矩阵动态追加

A = [];% 建立空矩阵for i = 1:10B = i^2;A = [A,B]; % 关键步骤end

应用:将A矩阵中的符合要求的数值存入B矩阵并画图表示

aaa = ARSP(:,2:3);% 将ARSP矩阵中的第二、三列存入aaa矩阵GoodairSpeed = [];for i = 1:length(aaa)if aaa(i,2) >= 7.9GoodairSpeed = [GoodairSpeed;aaa(i,:)];% 把符合要求的数据存入GoodairSpeed矩阵endendfigure(1)for i = 1:length(GoodairSpeed)x(i) = GoodairSpeed(i,1);y(i) = GoodairSpeed(i,2);plot(x,y,'k-')hold onendxlabel('Time/us');ylabel('Airspeed');

求欧氏距离norm()

norm([3,4]-[4,5])% 1.4142

向上取整 ceil()

该函数表示向上取整的意思,在数学中用符号“⌈ ⌉”表示

求矩阵中最大值 max()

参考文章

调试常用输出 disp()

注意输出数值时,需用 num2str() 函数转换为字符串

thetamin = 1disp(['最少线数的扫描角度: ',num2str(thetamin)]);

调试函数

参考文章

断点调试法

调试代码最经典的就是利用设置断点的方法,此处给出对应快捷键:

F12:设置/取消 断点F10:单步执行F11:单步执行,且碰到function跳入函数内执行,F10则不会跳入,这是二者的明显区别Shift + F11:跳入function之后,通过该指令推出functionF5:执行相邻两次断点见的所有指令,如:断点在for循环中,则F5一次,循环执行一次Shift + F5:退出断点调试

画图函数plot()

x = linspace(0,3);y = x.^2.*sin(x);plot(x,y);line([2,2],[0,2^2*sin(2)]);% 输入公式 Latex显示str = '$$ \int_{0}^{2} x^2\sin(x) dx $$';text(0.25,2.5,str,'Interpreter','latex');% annotation范围为图表的[0 1] X表示图表横坐标区间的32%-50%,Y表示图表纵坐标区间的60%-40%annotation('arrow','X',[0.32,0.5],'Y',[0.6,0.4]);

x = linspace(1,2);f = x.^2;g = sin(2*pi*x);plot(x,f,x,g);h = legend('$$ t^{2} $$','$$ \sin(2\pi t) $$');set(h,'Interpreter','latex');

对于添加带公式的图示也可以按照如下方法定义,注意图示只按顺序依次添加:

x = linspace(1,2);f = x.^2;g = sin(2*pi*x);plot(x,f,x,g);legend('$$ t^{2} $$','$$ \sin(2\pi t) $$','Interpreter','latex');

参考文章

求矩阵元素个数numel()

A = magic(4);A(:,:,2) = A';numel(A); % 32

矩阵内容堆叠repmat()

其功能是以A的内容堆叠在 (MxN) 的矩阵B中,B矩阵的大小由MxN及A矩阵的内容决定,如果A是一个3x4x5的矩阵,有B = repmat(A,2,3)则最后的矩阵是6x12x5

B = repmat(A,m,n)B = repmat(A,[m n])B = repmat(A,[m n p...])

函数执行feval()

参考文章

如果觉得《MATLAB 常用函数学习笔记》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。