5、利用rand函数产生(0,1)间的均匀分布的10*10随机矩阵A,然后统计A中大于等于0.6的元素的个数。
解:
A=rand(10);
B=A >= 0.6;
C=sum(B);
count=sum(C)
运行结果(每次运行结果是不同的,仅作参考):
count=32
6、利用randn函数产生均值为0,方差为1的10*10随机矩阵A,然后统计A中大于-0.5且小于0.5的元素的个数。
解:
A=randn(10);
B=(A<0.5)&(A>-0.5);
C=sum(sum(B))
运行结果(每次运行结果是不同的,仅作参考):C=48
1、解:
if and(a<1,b<=0.5)
语句1;
elseif and(a<1,b>0.5)
语句2;
elseif and(a>=1,b<=0.5)
语句3;
else
语句4;
2、有一矩阵A,找出矩阵中值等于1的元素,并将它们重新排列成列向量B。
解:
A=2*rand(4);
k=find(A<=1);
A(k)=[];%删除下标为k的元素
B=A
运行结果(每次运行结果是不同的,仅作参考)
B =
1.4769
1.8348
1.5310
1.1524
1.3667
1.0932
1.2889
1.2952
1.3580
3、在一测量矩阵A(100*3)中,存在有奇异值(假设大于100的置认为是奇异值),编程实
现删去奇异值所在的行。
解:
A=120*randn(10,3);
[i,j]=find(A>100);
A(i,:)=[] %删去存在奇异值的行
4、在给定的100*100矩阵中,删去整行为0的行,删去整列为0的列。
解:
A=diag([1 2 3 4],1)
B=any(A)
[i,j]=find(B==0)
A(:,i)=[] %删除全为0的列
B=any(A)
[i,j]=find(B==0)
A(j,:)=[] %删除全为0的行
运行结果:
初始值:A =
0 1 0 0 0
0 0 2 0 0
0 0 0 3 0
0 0 0 0 4
0 0 0 0 0
操作后:A =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
1、将窗口分割成四格,分别绘制正弦、余弦、正切和余切函数曲线,并加上适当的标注。
程序为:
x=0:pi/50:2*pi;
k=[1 26 51 76 101];
x(k)=[];%删除正切和余切的奇异点
figure(1)
subplot(2,2,1)
plot(x,sin(x),k--),grid on
legend(\ity=sin(x))
title(y=sin(x))
xlabel(x), ylabel(y)
subplot(2,2,2)
plot(x,cos(x),r--),grid on
legend(\ity=cos(x))
title(y=con(x))
xlabel(x), ylabel(y)
subplot(2,2,3)
plot(x,tan(x),k),grid on
legend(\ity=tan(x))
title(y=tan(x))
xlabel(x), ylabel(y)
subplot(2,2,4)
plot(x,cot(x),b-),grid on
legend(\ity=cot(x))
title(y=cot(x))
xlabel(x), ylabel(y)
运行如下:
2、绘制多峰函数peaks和三角函数多条曲线。
多峰函数peaks:
[x,y]=meshgrid(-3:0.15:3);
z=peaks(x,y);
x1=x(1,:);
figure(1)
plot(x1,z),grid on
title(二维多峰函数)
图形为:
[x,y]=meshgrid(-3:0.15:3);
z=peaks(x,y);
figure(1)
plot3(x,y,z),grid on
title(三维多峰函数)
三角函数多条曲线:
程序为:
t=-pi:pi/20:pi;
y1=sinh(t); %双曲正弦
y2=cosh(t); %双曲余弦
figure(1)
subplot(2,1,1)
plot(t,y1,r--,t,y2,k-),grid on
legend(\ity1=sinh(t),\ity2=cosh(t))
title(三角函数1)
xlabel(t), ylabel(y)
subplot(2,1,2)
plot(t,sin(t),k-),grid on
hold on %保持原有图像函数
plot(t,cos(t),r--)
legend(\ity2=cos(t),\ity1=sin(t))
title(三角函数2)
xlabel(t), ylabel(y)
运行图形为:
3、将图形窗口分成两个,分别绘制以下函数在[-3,3]区间上的曲线,并利用axis调整轴刻度,使他们具有相同缩放尺度。y1=2x+5; y2=x2-3x+1。
程序为:
x=-3:0.1:3;
y1=2*x+5;
y2=x.^2-3*x+1;
figure(1)
subplot(2,2,1)
plot(x,y1,r-),grid on
legend(\ity1=2*x+5)
title(y1=2x+5)
xlabel(x), ylabel(y1)
subplot(2,2,2)
plot(x,y2,k-),grid on
legend(\ity2=x.^2-3*x+1)
title(y2=x^2-3x+1)
xlabel(x), ylabel(y2)
subplot(2,2,3)
plot(x,y1,r-),grid on
legend(\ity1=2*x+5)
title(调整后的y1=2x+5)
axis([-3 3 -10 10])
xlabel(x), ylabel(y1)
subplot(2,2,4)
plot(x,y2,k-),grid on
legend(\ity2=x.^2-3*x+1)
title(调整后的y2=x^2-