matlab期末复习练习题8题

更新时间:2023-12-28 05:48:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1.囧

function RanDisplayJiong axis off;

set(gcf,'menubar','none','toolbar','none'); for k=1:100

h=text(rand,rand,...

['\\fontsize{',num2str(unifrnd(20,50)),'}\\fontname{隶书} 囧'],...

'color',rand(1,3),'Rotation',360*rand); pause(0.2); end

2.小猫

function T=cat_in_holl(n) T=zeros(1,n); for k=1:n

c=unidrnd(3,1); while c~=1 if c==2

T(k)=T(k)+4; else

T(k)=T(k)+6; end

c=unidrnd(3,1); end

T(k)=T(k)+2; end mean(T,2)

3.矩阵

function ex2_3_6slow A=unidrnd(100,1000000,7); B=zeros(1000000,3); tic

for m=1:1000000 a=A(m,:); b=[4 6 8]; for ii=1:3

dd=a(a==b(ii)); if isempty(dd)==0 b(ii)=0; end end B(m,:)=b; end

toc

4.三角形

function triangle_table fig =

figure('defaultuicontrolunits','normalized','name','triangle_table',...

'numbertitle','off','menubar','none'); ah = axes('Pos',[.1 .2 .75 .75],'Visible','off');

slider_h = uicontrol('style','slider','units','normalized','pos',...

[0.1,0.05,0.75,0.05],'sliderstep',[1/6,0.05],'callback',@change_color); hold on for k = 0:6

plot(0:6-k,(6-k)*ones(1,(7-k)),'k'); plot(k*ones(1,(7-k)),k:6,'k'); end

plot([0,6],[0,6],'k'); hold off; for x = 1:5 for y = 1:x

text(y-0.5,x+0.5,num2str(x),'color','k','tag','数字'); end end

for k = 0:5

text(k+0.1,k+0.5,[num2str(k),'.5'],'tag','数字'); end

%====slider's callback function(nested function)====== function change_color(hObject,eventdata) v = round(6*get(slider_h,'value')); num_h = findobj('tag','数字'); num_pos = get(num_h,'pos');

red_num_logic = cellfun(@(x) (x(1)<=v&&x(2)<=v),num_pos); set(num_h(red_num_logic),'color','r'); set(num_h(~red_num_logic),'color','k'); end end

5.画图象

%p82 例5.4-3

function [m,n,TT]=plot3dnmT(N,L) C=zeros(N,1); m=linspace(0,2,L); [m,n]=meshgrid(m,m); TT=zeros(size(n));

for ii=1:L for jj=1:L

TT(ii,jj)=calcT(m(ii,jj),n(ii,jj)); end end

function Tmn=calcT(mm,nn) for N1=1:N

C(N1)=(mm^N1/gamma(N1+1))*sum(nn.^(0:N1-1)./gamma(1:N1)); Tmn=1.0-exp(-mm-nn)*sum(C); end end

mesh(n,m,TT); end

6.

7.读取图像

function DuckLakeSegmentation %导入图像文件引导对话框

[filename,pathname,flag] = uigetfile('*.jpg','请导入图像文件'); Duck = imread([pathname,filename]);

LakeTrainData = [52 74 87;76 117 150;19 48 62;35 64 82;46 58 36;... 50 57 23;110 127 135;156 173 189;246 242 232;... 166 174 151];%从图上选取的几个位于湖面区域的有代表的点的RGB值

%从图上选取的几个位于鸭子区域的有代表的点的RGB值 DuckTrainData = [211 192 107;202 193 164;32 25 0;213 201 151;115 75 16;... 101 70 0;169 131 22;150 133 87]; %属于湖的点为0,鸭子为1 group =

[zeros(size(LakeTrainData,1),1);ones(size(DuckTrainData,1),1)]; LakeDuckSVM = svmtrain([LakeTrainData;DuckTrainData],group,... 'Kernel_Function','polynomial','Polyorder',2);%训练得到支持向量分类机 [m,n,k] = size(Duck);

Duck1 = double(reshape(Duck, m*n, k));%将数组Duck转成m*n行,3列的双精度矩阵

%根据训练得到的支持向量机对整个图像的像素点分类 IndDuck = svmclassify(LakeDuckSVM,Duck1); IndLake = ~IndDuck;%属于湖的点的逻辑数组

result = reshape([IndLake, IndLake, IndLake],[m,n,k]);%和图片的维数相对应 Duck2 = Duck; Duck2(result) = 0; figure imshow(Duck2)

8.定时器

function example12_2_1 TimerA =

timer('timerfcn',@TimerFun,'StartDelay',unidrnd(5000,1)/1000,... 'executionmode','fixedrate');

%用来记录TimerFun函数执行的次数,由于通过StartDelay方式实现每隔随机一段时间进行特定

%操作时,需要反复停止与启动定时器,因此TasksExecuted就不准确了。 set(TimerA,'userdata',0); num = 5;%随机显示5次后,就停止定时器 start(TimerA)

function TimerFun(obj,event)

set(TimerA,'userdata',get(TimerA,'userdata')+1);

disp(['间隔时间为 ',num2str(get(TimerA,'startDelay')),' S!']); disp('Hello World!'); stop(TimerA);

set(TimerA,'StartDelay',unidrnd(5000,1)/1000); start(TimerA);

while get(TimerA,'userdata')==num stop(TimerA) return; end end end

本文来源:https://www.bwwdw.com/article/49nx.html

Top