MATLAB基础(1)

矩阵

  1. 直接导入

    A = [1,2;3,4;]
    
  2. 文件导入

    A = load('文件名')
    
  3. 读取Excel数据文件

    num = xlsread('文件名')
    num = xlsread('文件名',sheet) 可以选择所在的sheet
    num = xlsread('文件名',range) 可以选择所在单元格范围
    num = xlsread('文件名',sheet,range) 同时限制
    num = xlsread('文件名',...,functionhandle) 把所获得的数据给指定的函数句柄
    
  4. 用MATLAB内部函数生成矩阵

    1. zeros(3,2) 3行2列的零矩阵
      
    2. ones(3,2) 3行2列的一矩阵
      
    3. 用m文件生成
      edit A.m
      要运行文件只需在命令窗口中写下文件名即可
      
    4. randn(3,2) 3行2列随机正态分布矩阵
      

tips:

dir 显示当前文件夹下的所有文件
A(m,n) 获得矩阵A第m行n列的元素
A(x) 获得矩阵A第x个元素(先数列)
A(1:k,j) 矩阵A 的第 j 列的前 k 个元素
A(:,end)} 矩阵A 的最后一列
A(:,[1 3 2 4]) 矩阵A 的第 1,3,2,4 列
A' 矩阵A转置

数组

	1	:	2	:	8
  start : step  :  end
第一个元素1:数组步长2:不能超过8
没有写步长就默认是1

遍历数组的两种方式

一,普通数组:

1,下标遍历:

 v=[1,4,6,4,1];  
 s=0;  
 for i=1:length(v)  
 	s=s+v(i);  
 end  
 disp(s); 

结果:16

2,元素遍历:

 v=[1,4,6,4,1];  
 s=0;  
 for ve=v   
 	s=s+ve;  
 end  
 disp(s); 

结果: 16

二,元胞数组:

1,下标遍历

 strvec={'i','am','iwantnon'};  
 str=[];  for i=1:length(strvec)   
 str=[str,' ',strvec{i}]; 
 end 
 disp(str); 

结果:i am iwantnon

2,元素遍历:

 strvec={'i','am','iwantnon'}; 
 str=[]; 
 for s=strvec  
 str=[str,' ',s{1}];
 end
 disp(str); 

结果:i am iwantnon

注:二,2中的s是1*1 cell,要访问之需用s{1}。

元素遍历的一个应用:

如果被遍历数组的元素本身是下标(例如find函数的返回值index数组),那么用元素遍历更自然:

 index=find(A==0);
 for i=index  
	 A(i)=...; 
 end

MATLAB 绘图

基本绘图环境及参数设置

平面绘图函数 plot 的一般使用格式为

plot(x,y,’color-style-marker’)

颜色包括 ‘y’, ‘r’, ‘g’, ‘b’, ‘k’ 等.

线型包括 ‘-’, ‘–’, ‘:’, ‘-.’ 等.

标记包括 ‘+’, ‘0’, ‘*’, ‘x’, ‘s’, ‘d’ 等.

x = 0:pi/10:2*pi;
y = sin(x);
plot(x,y, '--rd')
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine Function','FontSize',12)

在这里插入图片描述

image-20210704162417183

在一个图形中绘制多组数据

x = 0:pi/100:2*pi;
y = sin(x);
y2 = sin(x-.25);
y3 = sin(x-.5);
plot(x,y,x,y2,x,y3)
legend(’sin(x)’,’sin(x-.25)’,’sin(x-.5)’)

在这里插入图片描述

image-20210704162619239

*•*figure 打开绘制图形的窗口.

clf 清除图形.

hold on, hold off 在绘制新图形前保留或者清除当前图形.

grid on, grid off 在图形中显示或者关闭网格线.

axis on, axis off 打开或者关闭坐标轴.

title(’string’) 图形标题.

xlabel(’string’), ylabel(’string’) x, y 坐标轴的标志.

legend(’string1’, ’string2’),… 图例说明.

gtext(’string’) 用鼠标在指定点出标记.

ginput() 用鼠标在图形中取点的坐标.

在当前图形中添加新图形

[x,y,z] = peaks;
pcolor(x,y,z)
shading interp
hold on
contour(x,y,z,20,’k’)
hold off

在一个图形中分区绘图

t = 0:pi/10:2*pi;

[X,Y,Z] = cylinder(4*cos(t));

subplot(2,2,1); mesh(X)

subplot(2,2,2); mesh(Y)

subplot(2,2,3); mesh(Z)

subplot(2,2,4); mesh(X,Y,Z)

极坐标绘图

MATLAB 还可以在极坐标系中绘制各种图形. 极坐标绘图函数是polar, 其一般使用格式是

polar(t, r), 使用极角 t 和极径 r 绘制图形.

polar(t, r, ’linespec’), 可以设置线型, 颜色等性质.

例如, 绘制图形 ρ = |sin(t)| 的图形

t=0:pi/50:2*pi;

polar(t, abs(sin(4*t)), 'r')

在这里插入图片描述

image-20210704163351432

其它二维绘图指指令

绘制函数绘图 fplot, 例如

​ fplot(’cos(1./x)’,[-1,1])

简易绘图函数 ezplot, 例如

​ ezplot(’2/3exp(-t/2)cos(3/2t)’,[0,4pi])

绘制等高线 contour, 例如

​ [X,Y,Z]=peaks(n);

​ contour(X,Y,Z,10)

饼形图 pie, 例如

​ explode=[0 0 0 0 1];

​ pie(x,explode)

直方图 hist, 例如

​ hist (rand (10, 3))

三维曲线绘图

三维曲线绘图函数 plot3, 例如

t=0:pi/10:20*pi;
x=t.*sin(t);
y=t.*cos(t);
z=t;
plot3(x,y,z)

以及例子

t = (0:0.02:2)*pi;
x = sin(t); y = cos(t); z = cos(2*t);
plot3(x, y, z, 'b-', x, y, z,'bd'),
view([-82,58]), box on, legend('Ring', 'Diamond'),

空间曲面绘图函数包括 mesh 及 surf

[X,Y]=meshgrid(-8:.5:8);

R=sqrt(X.^2+Y.^2)+eps;

Z=sin(R)./R;

mesh(X,Y,Z)
x=-4:4; y=x;
[X,Y]=meshgrid(x,y);
Z=X.^2+Y.^2;
surf(X,Y,Z);
hold on, colormap(hot)
stem3(X,Y,Z,’bo’)
[X0,Y0,Z0]=sphere(30);
X=2*X0; Y=2*Y0; Z=2*Z0;
surf(X0,Y0,Z0);
shading interp;
hold on;
mesh(X,Y,Z), colormap(hot),
hold off, hidden off, axis equal, axis off
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值