matlab通过的例子.docx
《matlab通过的例子.docx》由会员分享,可在线阅读,更多相关《matlab通过的例子.docx(22页珍藏版)》请在冰豆网上搜索。
matlab通过的例子
例1:
日本数学家角谷提出一个著明的猜想:
任何一个大于1的自然数经过如下的有限步运算,最后结果为1
(1)如果是偶数,将其除以2
(2)
(2)如过是奇数,将其乘以3,再加上1
例如n=10
10/2=5
5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1
n=input('Typen:
'); %从键盘输入n,并赋值给n
i=1; %标记运算的步数
f=[n];%存放每次运算后的n的值
whilen~=1&n>0
ifmod(n,2)==0
n=n/2;
elsen=n*3+1;
end
i=i+1;
f(i)=n;
end
f %输出每次运算后的n的值组成的集合
I=i-1 %输出运算的步数,f集合中会有i个元素,但实际上是只经过i-1步的运算
n %输出最后一步所得n的值,显然,一般n=1,除非猜想不成立时,n会取到其它的值!
例2:
曲线转换按钮
h0=figure('toolbar','none','position',[200150450250],'name','例2');
x=0:
0.5:
2*pi;
y=sin(x);
h=plot(x,y);
gridon
huidiao=[...
'ifi==1,',...
'i=0;,',...
'y=cos(x);,',...
'delete(h),',...
'set(hm,''string'',''正弦函数''),',...
'h=plot(x,y);,',...
'gridon,',...
'elseifi==0,',...
'i=1;,',...
'y=sin(x);,',...
'set(hm,''string'',''余弦函数''),',...
'delete(h),',...
'h=plot(x,y);,',...
'gridon,',...
'end,',...
'end'];
hm=uicontrol(gcf,'style','pushbutton',...
'string','余弦函数',...
'callback',huidiao);
i=1;
set(hm,'position',[250206020]);
%set(gca,'position',[0.20.20.60.6])
title('按钮的使用')
holdon
例3:
栅格控制按钮
h0=figure('toolbar','none','position',[200150450250],'name','例3');
x=0:
0.5:
2*pi;
y=sin(x);
plot(x,y)
huidiao1=[...
'set(h_toggle2,''value'',0),',...
'gridon,',...
];
huidiao2=[...
'set(h_toggle1,''value'',0),',...
'gridoff,',...
];
h_toggle1=uicontrol(gcf,'style','togglebutton',...
'string','gridon',...
'value',0,...
'position',[20455020],...
'callback',huidiao1);
h_toggle2=uicontrol(gcf,'style','togglebutton',...
'string','gridoff',...
'value',0,...
'position',[20205020],...
'callback',huidiao2);
set(gca,'position',[0.20.20.60.6])
title('开关按钮的使用')
例4:
编辑框的使用
h0=figure('toolbar','none','position',[200150350250],'name','例4');
f='Pleaseinputtheletter';huidiao1=[...
'g=upper(f);,',...
'set(h2_edit,''string'',g),',...
];
huidiao2=[...
'g=lower(f);,',...
'set(h2_edit,''string'',g),',...
];
h1_edit=uicontrol(gcf,'style','edit',...
'position',[10020010050],...
'HorizontalAlignment','left',...
'string','Pleaseinputtheletter',...
'callback','f=get(h1_edit,''string'');',...
'background','w',...
'max',5,...
'min',1);
h2_edit=uicontrol(gcf,'style','edit',...
'HorizontalAlignment','left',...
'position',[10010010050],...
'background','w',...
'max',5,...
'min',1);
h1_button=uicontrol(gcf,'style','pushbutton',...
'string','小写变大写',...
'position',[1004510020],...
'callback',huidiao1);
h2_button=uicontrol(gcf,'style','pushbutton',...
'string','大写变小写',...
'position',[1002010020],...
'callback',huidiao2);
例5:
弹出式菜单
h0=figure('toolbar','none',...
'position',[200150450250],...
'name','例5');
x=0:
0.5:
2*pi;
y=sin(x);
h=plot(x,y);
gridon
hm=uicontrol(gcf,'style','popupmenu',...
'string',...
'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',...
'position',[250205020]);
set(hm,'value',1)
huidiao=[...
'v=get(hm,''value'');,',...
'switchv,',...
'case1,',...
'delete(h),',...
'y=sin(x);,',...
'h=plot(x,y);,',...
'gridon,',...
'case2,',...
'delete(h),',...
'y=cos(x);,',...
'h=plot(x,y);,',...
'gridon,',...
'case3,',...
'delete(h),',...
'y=sin(x)+cos(x);,',...
'h=plot(x,y);,',...
'gridon,',...
'case4,',...
'delete(h),',...
'y=exp(-sin(x));,',...
'h=plot(x,y);,',...
'gridon,',...
'end'];
set(hm,'callback',huidiao)
set(gca,'position',[0.20.20.60.6])
title('弹出式菜单的使用')
例6:
多选菜单
h0=figure('toolbar','none',...
'position',[200150450250],...
'name','实例6');
[x,y]=meshgrid(-8:
0.5:
8);
r=sqrt(x.^2+y.^2)+eps;
z=sin(r)./r;
h0=mesh(x,y,z);
hlist=uicontrol(gcf,'style','listbox',...
'string','default|spring|summer|autumn|winter',...
'max',5,...
'min',1,...
'position',[202080100],...
'callback',[...
'k=get(hlist,''value'');,',...
'switchk,',...
'case1,',...
'colormapdefault,',...
'case2,',...
'colormapspring,',...
'case3,',...
'colormapsummer,',...
'case4,',...
'colormapautumn,',...
'case5,',...
'colormapwinter,',...
'end']);
例7:
菜单控制的使用
h0=figure('toolbar','none',...
'position',[200150450250],...
'name','实例7');
x=0:
0.5:
2*pi;
y=cos(x);
h=plot(x,y);
gridon
set(gcf,'toolbar','none')
hm=uimenu('label','example');
huidiao1=[...
'set(hm_gridon,''checked'',''on''),',...
'set(hm_gridoff,''checked'',''off''),',...
'gridon'];
huidiao2=[...
'set(hm_gridoff,''checked'',''on''),',...
'set(hm_gridon,''checked'',''off''),',...
'gridoff'];
hm_gridon=uimenu(hm,'label','gridon',...
'checked','on',...
'callback',huidiao1);
hm_gridoff=uimenu(hm,'label','gridoff',...
'checked','off',...
'callback',huidiao2);
例8:
UIMENU菜单的应用
h0=figure('toolbar','none',...
'position',[200150450250],...
'name','例8');
h1=uimenu(gcf,'label','函数');
h11=uimenu(h1,'label','轮廓图',...
'callback',[...
'set(h31,''checked'',''on''),',...
'set(h32,''checked'',''off''),',...
'[x,y,z]=peaks;,',...
'contour3(x,y,z,30)']);
h12=uimenu(h1,'label','高斯分布',...
'callback',[...
'set(h31,''checked'',''on''),',...
'set(h32,''checked'',''off''),',...
'mesh(peaks);,',...
'axistight']);
h13=uimenu(h1,'label','Sinc函数',...
'callback',[...
'set(h31,''checked'',''on''),',...
'set(h32,''checked'',''off''),',...
'[x,y]=meshgrid(-8:
0.5:
8);,',...
'r=sqrt(x.^2+y.^2)+eps;,',...
'z=sin(r)./r;,',...
'mesh(x,y,z)']);
h2=uimenu(gcf,'label','色彩');
hl2
(1)=uimenu(h2,'label','Default',...
'checked','on',...
'callback',...
[...
'set(hl2,''checked'',''off''),',...
'set(hl2
(1),''checked'',''on''),',...
'colormap(''default'')']);
hl2
(2)=uimenu(h2,'label','spring',...
'callback',...
[...
'set(hl2,''checked'',''off''),',...
'set(hl2
(2),''checked'',''on''),',...
'colormap(spring)']);
hl2(3)=uimenu(h2,'label','Summer',...
'callback',...
[...
'set(hl2,''checked'',''off''),',...
'set(hl2(3),''checked'',''on''),',...
'colormap(summer)']);
hl2(4)=uimenu(h2,'label','Autumn',...
'callback',...
[...
'set(hl2,''checked'',''off''),',...
'set(hl2(4),''checked'',''on''),',...
'colormap(autumn)']);
hl2(5)=uimenu(h2,'label','Winter',...
'callback',...
[...
'set(hl2,''checked'',''off''),',...
'set(hl2(5),''checked'',''on''),',...
'colormap(winter)']);
h3=uimenu(gcf,'label','坐标选项');
h31=uimenu(h3,'label','Axison',...
'callback',...
[...
'axison,',...
'set(h31,''checked'',''on''),',...
'set(h32,''checked'',''off'')']);
h32=uimenu(h3,'label','Axisoff',...
'callback',...
[...
'axisoff,',...
'set(h32,''checked'',''on''),',...
'set(h31,''checked'',''off'')']);
例10:
用MATLAB,源程序如下:
clear;
A=imread('e:
\张儒良图像处理课件\water.jpg');
subplot(1,2,1);
subimage(A);
title('变化前的图');
A=im2double(A);%将A转换成double型
A=A/256;
R=A(:
:
1);
G=A(:
:
2);
B=A(:
:
3);
I=(R+G+B)/3;
S=1-min(R,min(G,B))./I;
H=zeros(size(S));%为H分配空间
t=find(S==0.0);%当S为时,H=0.0
H(t)=0.0;
t=find(S~=0.0);
H(t)=((R(t)-G(t))+(R(t)-B(t)))/2.0;
H(t)=H(t)./sqrt((R(t)-G(t)).^2+(R(t)-B(t)).*(G(t)-B(t)));
H(t)=acos(H(t));
t=find(B>G);
H(t)=2*pi-H(t);%转换成HSI
H=H+40/180*pi;%色调加40度
t=find(H>2*pi);
H(t)=H(t)-2*pi;
find(0<=H&H<=2*pi/3);
B(t)=I(t).*(1-S(t));
R(t)=I(t).*(1+S(t).*cos(H(t))./cos(pi/3-H(t)));
G(t)=3*I(t)-(B(t)+R(t));
t=find(2*pi/3R(t)=I(t).*(1-S(t));
G(t)=I(t).*(1+S(t).*cos(H(t)-2*pi/3)./cos(pi-H(t)));
B(t)=3*I(t)-(R(t)+G(t));
t=find(4*pi/3G(t)=I(t).*(1-S(t));
B(t)=I(t).*(1+S(t).*cos(H(t)-4*pi/3)./cos(5*pi/3-H(t)));
R(t)=3*I(t)-(G(t)+B(t));%转换成RGB
A(:
:
1)=R;
A(:
:
2)=G;
A(:
:
3)=B;
A=A*256;
A=im2uint8(A);%将A转换成uint8型
subplot(1,2,2);
subimage(A);
title('转换后的图');
运行之后结果为:
MATLAB报错:
>>z2
Warning:
Dividebyzero.
(Type"warningoffMATLAB:
divideByZero"tosuppressthiswarning.)
>InF:
\tools\matlab\work\z2.matline12
Warning:
Dividebyzero.
(Type"warningoffMATLAB:
divideByZero"tosuppressthiswarning.)
>InF:
\tools\matlab\work\z2.matline18
>>
是因为I有可能为0,但I==0时,S也没有意义,所以没有关系。
例11:
随机生成一个6行10列矩阵,矩阵的每个元素在0与1之间,如果哪个元素大于等于0.5,则把这个元素变为1,如果哪个元素小于0.5,则把这个元素变为0.
解法1:
ra=rand(6,10);
r=ra;
fori=1:
6
forj=1:
10
ifra(i,j)<0.5
ra(i,j)=0;
else
ra(i,j)=1;
end
end
end
r
ra
解法2:
ra=rand(6,10);
r=ra;
t=find(ra>=0.5);
ra(t)=1;
m=find(ra<0.5);
ra(m)=0;
r
ra
例12:
利用程序绘制颜色渐变的图像
forj=1:
30
fori=1:
20
a(i,j)=j;
end
end
image(a)
自编函数示例1:
例13:
编制函数plot2D,使其能输入参数控制曲线的绘制区间。
functionplot2D(a,b)
x=a:
0.1:
b;
y1=sin(x);
y2=cos(x);
y=y1-y2;
plot(x,y)
函数须用plot2D.m保存。
主函数
subplot(1,4,1);
plot2D(2,7);
subplot(1,4,2)
plot2D(2,6);
subplot(1,4,3)
plot2D(-10,10);
subplot(1,4,4);
plot2D(5,7)
自编函数示例2:
functionplot2D
x=-2:
0.1:
2;
y1=sin(x);
y2=cos(x);
y=y1-y2;
plot(x,y)
函数须用plot2D.m保存。
在命令窗口输入plot2D;
自编函数示例3:
functionw=f(x,y,z)
w=x.^3-2*y.^2-2*z+5;
函数须用f.m保存。
在命令窗口输入f(1,2,3)
例14:
利用imread()读取图像
a=imread('e:
\张儒良图像处理课件\water.jpg');
a
s=size(a);
s
例15:
二值图像
解法1:
RGB=imread('E:
\张儒良图像处理课件\070.tif');
BW=im2bw(RGB,0.5);
subplot(1,2,1);subimage(RGB);title('真彩图')
subplot(1,2,2);subimage(BW);title('二值图')
解法2:
RGB=imr