1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
s=[1 3 6 8 9 11 14 16]; A(s)=0 A =
0 0 5 7 0 0 13 15 2 4 0 0 10 12 0 0
3、写出下列指令运行结果。 A=[1,2;3,4]; B=[-1,-2;2,1]; S=3; A.*B A*B
S.*A S*B ans =
-1 -4 6 4 ans =
3 0 5 -2 ans =
3 6 9 12 ans =
-3 -6 6 3
4、下面的函数主要完成什么功能? function f=factor(n)
if n<=1 f=1;
else
f=factor(n-1)*n;
end
利用函数的递归调用,求n!。
5、写出下列指令运行结果。 ch=‘ABc123d4e56Fg9’; subch=ch(1:5) revch=ch(end:-1:1)
k=find(ch>=‘a’&ch<=‘z’); ch(k)=ch(k)-(‘a’-‘A’); char(ch)
length(k)
subch = ABc12 revch =
9gF65e4d321cBA ans =
ABC123D4E56FG9
6、写出下列指令运行结果。 A(1,1)={'this is cell'}; A{1,2}={[1 2 3;4 5 6]}; A{2,1}=[1+2*i];
A{2,2}=A{1,2}{1}+(A{1,2}{1}(1,1)+A{1,2}{1}(2,2)); celldisp(A)
A{1,1} = this is cell A{2,1} =
1.0000 + 2.0000i A{1,2}{1} =
1 2 3 4 5 6 A{2,2} =
7 8 9 10 11 12
7、在同一个图中绘制两个函数,这两个函数分别是: y=e(-t/3)
y0=e(-t/3)*sin(3t)
其中y是用红色的细实线,而y0是用兰色的虚线绘制,t的区间是(0:4?),
t的步长为?/50,t轴和y轴分别用斜粗题指示,图中有网格表示。 四、编程题 1、答案
clf
x=0:pi/200:2*pi; y1=sin(x); y2=cos(x);
zz=x(find(abs(y1-y2)<0.005)) z=min(zz)
plot(x,y1,'r-',x,y2,'g-.') hold on
plot(zz,sin(zz),'*')
legend('sin','cos')
2、答案
t=(0:pi/100:pi)'; y1=sin(t)*[1,-1]; y2=sin(t).*sin(9*t); t3=pi*(0:9)/9;
y3=sin(t3).*sin(9*t3);subplot(1,2,1) plot(t,y1,'r:',t,y2,'b',t3,y3,'bo') subplot(1,2,2) plot(t,y2,'b') axis([0,pi,-1,1])
3、答案
price=input('请输入商品价格'); switch fix(price/100)
case {0,1} %价格小于200 rate=0;
case {2,3,4} %价格大于等于200但小于500 rate=3/100;
case num2cell(5:9) %价格大于等于500但小于1000 rate=5/100;
case num2cell(10:24) %价格大于等于1000但小于2500 rate=8/100;
case num2cell(25:49) %价格大于等于2500但小于5000 rate=10/100;
otherwise %价格大于等于5000 rate=14/100;
end
price=price*(1-rate) %输出商品实际销售价格
4、答案
function f=fab(n)
if (n==1) f = 1; elseif (n==2) f =2; else
f = fab(n-1) + fab(n-2);
end
5、答案 str1 = ''; str2 = ''; str3= '';
val = cell(5,2) a=zeros(5,1);
ave = 0;
student = struct('Name',str1,'No',str2,'Scores',val,'Ave',ave); n = input('please input students number:'); for i=1 : n
str1 = input('Name:'); str2 = input('No.:'); %for k=1:2
% val(k,:) = input('Curriculums and Scores:'); % a(k,1)=val{k,2}; %end
%val(1,:) = input('Curriculum:'); %val(:,2) = input('Scores:');
val = input('please input five Curriculums and Scores:'); for k=1:5
a(k,1)=val{k,2}; end
student(i).Name = str1; student(i).No = str2; student(i).Scores = val;
student(i).Ave = mean(a); end
for ii= 1:(length(student)-1) iptr = ii;
for jj=ii+1 : length(student)
if (student(jj).Ave > student(iptr).Ave) iptr = jj; end end
if ii ~=iptr
temp = student(ii);
student(ii) =student(iptr); student(iptr) = temp; end end
for ii=1 : length(student) if student(ii).Ave > 80 disp(student(ii).Name); disp(student(ii).Ave); end end
disp(['student name',blanks(6),'student no.',blanks(6),'student average']);disp(' ')
for ii=1 : length(student)
disp([student(ii).Name,blanks(20),student(ii).No,blanks(20),num2str(student(ii).Ave)]); end