A = pascal(7) %生成7*7 pascal矩阵
B = A([1,3,5],[5,6,7]) %构成矩阵B
C = A(5:7,1:3)%构成矩阵C
D = B-1*C
A =
1 1 1 1 1 1 1
1 2 3 4 5 6 7
1 3 610152128
1 41020355684
1 5153570 126 210
1 62156 126 252 462
1 72884 210 462 924
B =
1 1 1
152128
70 126 210
C =
1 515
1 621
1 728
D =
0-4 -14
1415 7
69 119 182
第二题:
function A=prime_number()
N=100
j=1
for i=2:N
if prod(rem(i,2:(i-1)))~=0
A(j)=i
j=j+1
end
end
A =
2 3 5 7111317192329313741434753596167717379838997
1输出x,y两个中值较大的一个值x=input(‘x’)
y=input(‘y’)
if x>y
x
else
y
end
2输入x,计算y的值。计算函数的值y=x+1,x<0,y=2x-1,x≧0
x=input错误!未指定书签。(‘x)
if x<0
y=x+1
else
y=2*x-1
end
3输入一学生成绩,评定其等级,方法是: 90~100分为“优秀”,80~89分为“良好”, 70~79分为“中等”,60~69分为“及格”, 60分为“不合格”
%%(1)题I=imread('abc.jpg')
II=rgb2gray(I)
imshow(II)
for i=1:size(II,1)
for j=1:size(II,2)
if (0<=II(i,j) &&II(i,j)<=50)
III(i,j)=2
elseif (50<II(i,j) &&II(i,j)<=100)
III(i,j)=8
elseif (100<II(i,j) &&II(i,j)<=150)
III(i,j)=16
elseif (150<II(i,j) &&II(i,j)<=200)
III(i,j)=64
elseif(200<II(i,j) &&II(i,j)<=250)
III(i,j)=128
end
end
end
figure
imshow(III)
%%(2)题
x=linspace(0,2*pi,500)
for i=1:length(x)
if (x(i)<=pi)
y1(i)=1
else
y1(i)=0
end
if (x(i)<=pi)
y2(i)=x(i)/pi
else
y2(i)=(2*pi-x(i))/pi
end
y3(i)=sin(x(i))
end
figureplot(x,y1)
figureplot(x,y2)
figureplot(x,y3)
figure
subplot(1,3,1)plot(x,y1)
subplot(1,3,2)plot(x,y2)
subplot(1,3,3)plot(x,y3)
figureplot(x,y1,x,y2,x,y3)legend('方波','三角波','正弦')
%%(3)题
eps=0.001
xl=3fl=xl^3-xl^2-6*xl-7 %不支持函数就直接写。
xr=4fr=xr^3-xr^2-6*xr-7
while (1)
xm=(xl+xr)/2fm=xm^3-xm^2-6*xm-7
if (abs(fm)<eps)
break
elseif (fm*fl<0)
xr=xmfr=fm
else
xl=xmfl=fm
end
end
x=xm
f(x)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)