MATLAB把三个根据邻接矩阵画图放在一个根据邻接矩阵画图Z中,画图后有错误

当前位置:
&用matlab出现“不合适的矩阵安排”问题,求具体修改方法和错误根源
用matlab出现“不合适的矩阵安排”问题,求具体修改方法和错误根源
作者 ruanjayrock
源代码如下:
& && && && && &&&见附件。
关于代码的一些注释:
& && && && && && && && && && && &&&%问题出在这个while(gen&10)大循环的while(1)里面 代码目的是多目标优化,寻找方程组最小值。x1 x2是随机生成的未知数,方程为f1 f2 f3
& && && && && && && && && && && && &%我先随机找到三个方程的各一个值,然后用x1 x2产生的值去除他们,得到3组数据差异不太大的数,放到D矩阵中。
& && && && && && && && && && && && &%然后找到D中三个函数值的和最小的放到F矩阵中,并消除D中已经放进F的行。
& && && && && && && && && && && && &%while循环是对D中剩余数据处理,让他们与原来D中和最小的数的三个函数值进行比较,寻找全小于,&&&.&&&,&&&关系的行,并将其放到
& && && && && && && && && && && && &%F矩阵中,并在D中消去这些行,如果循环完一次,F中添加0行,以区分。再从头开始,直到D矩阵变成空矩阵。然后gen+1,继续这个过程。
& && && && && && && && && && && && &%直到满足gen的条件。所以D矩阵与F矩阵都是
& && && && && && && && && && && && &%动态矩阵,出现的错误就是在数据拷贝的时候提示“不合理的空矩阵安排”。
希望大神能帮我找到问题,并且给点修改指导,多谢多谢!
把程序贴出来,然后把错误的所有信息都贴出来,报错错误信息,错误发生的地方,错误的来源等,这样别人才好帮你排除问题,你提供的信息相当于没有任何信息~~
引用回帖:: Originally posted by dnp at
把程序贴出来,然后把错误的所有信息都贴出来,报错错误信息,错误发生的地方,错误的来源等,这样别人才好帮你排除问题,你提供的信息相当于没有任何信息~~ 好的,谢谢!本来是程序太多了,怕太占版面
具体的程序代码如下:
x1=single(3*(-1+2*rand(1,CC)));
x2=single(3*(-1+2*rand(1,CC)));
f1=0.5*(x1.^2+x2.^2)+sin(x1.^2+x2.^2);
f2=((3*x1-2*x2+4).^2)/8+((x1-x2+1).^2)/27+15;
f3=1./(x1.^2+x2.^2+1)-1.1*exp(-x1.^2-x2.^2);
cigema=0.0125;
A=[x1',x2',f1',f2',f3'];
xx=randi([1,CC]);
D=[x1',x2',A(:,3)./C(3),A(:,4)./C(4),A(:,5)./C(5),abs(A(:,3)./C(3))+abs(A(:,4)./C(4))+abs(A(:,5)./C(5))];
[E,P]=min(D(:,6));
F(j,=D(P,;
m=size(D,1);
K=zeros(1,m);
FF=zeros(CCC,7);
while(gen&100)
& && && && && && && && && && && && && && && & %问题出在这两个while循环中
& && && && & for i=1:m& &
& && && && && && &if(((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c))||...
& && && && && && &&&((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c))||...
& && && && && && &&&((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c))||...
& && && && && && &&&((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c)))%||...
& && && && && && &&&%((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c))||...
& && && && && && &&&%((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c))||...
& && && && && && &&&%((D(i,3)&a)&&(D(i,4)&b)&&(D(i,5)&c)))
& && && && && && &F(j,=D(i,;
& && && && && && &K(i)=1;
& && && && && && &j=j+1;
& && && && && && & end
& && && && & end&&
& && && && & for i=1:m
& && && && && &&&if K(i)~=1
& && && && && && && &EE(t,=D(i,;
& && && && && && && &t=t+1;
& && && && && &&&end
& && && && & end
& && && && & D(1:t-1,=EE(1:t-1,;
& && && && & D(t:m,=[];
& && && && & K=zeros(1,t-1);
& && && && & t=1;
& && && && && &&&F(j,=0;
& && && && && &&&KK(j)=1;
& && && && && &&&j=j+1;
& && && && &&&[E,P]=min(D(:,6));&&
& && && && && &&&F(j,=D(P,;
& && && && && &&&a=D(P,3);
& && && && && &&&b=D(P,4);
& && && && && &&&c=D(P,5);& && &
& && && && && &&&D(P,=[];
& && && && && &&&m=size(D,1);& && && && && && && && && && && &
& && && && && &&&K=[K,zeros(1)];
& && && && && &&&j=j+1;
& && && && && &&&if m==0
%& && && && && && && & F(j,=0;
%& && && && && && && & KK(j)=1;
%& && && && && && && & j=j+1;
%& && && && && && && & F(j,=D(1,;
& && && && && && && &
& && && && && &&&end
& && && && &
m=size(F,1);
mm=size(KK,2);
for i=1:mm
& & if KK(i)==1
& && &&&KKK(1:i-t,=F(t:i-1,;
& && &&&KKK=sortrows(KKK,6);
& && &&&F(t:i-1,=KKK(1:i-t,;
& && &&&KKK=[];
& && &&&t=i+1;
& && &&&KKK(t:m,=F(t:m,;
& && &&&KKK=sortrows(KKK,6);
& && &&&F(t:i,=KKK(t:i,;
&&%分配适应度 最小的值适应度为0.9 最大的值适应度为0.1
KKKK=zeros(m,1);
count=0.8/(CC-1);
& &&&if F(i,==0
& && && &t=t+1;
& && && &KKKK(i)=0.9-count*(i-1-t);
F=[F,KKKK];
F(find(sum(abs(F'))==0),=[];
m=size(F,1);
& & t=0.1+0.8*rand(1);
& & for mm=1:m
& && &&&if F(mm,7)&t
& && && && &FF(count,=F(mm,;
& && && && &count=count+1;
& && && && &if(count&CCC)
& && && && && &
& && && && &end
& && &&&end
& & if(count&CCC)
& &for m=1:20
& && & if (rand(1)&0.8)
& && && &RR=randperm(10);
& && && &RRR=zeros(3,7);
& && && &RRR(1,1)=FF(RR(1),1)+FF(RR(2),1);
& && && &RRR(1,2)=FF(RR(1),2)+FF(RR(2),2);
& && && &RRR(2,1)=1.5*FF(RR(1),1)-0.5*FF(RR(2),1);
& && && &RRR(2,2)=1.5*FF(RR(1),2)-0.5*FF(RR(2),2);
& && && &RRR(3,1)=1.5*FF(RR(2),1)-0.5*FF(RR(1),1);
& && && &RRR(3,2)=1.5*FF(RR(2),2)-0.5*FF(RR(1),2);
& && &&&for i=1:3
& && && &&&RRR(i,3)=(0.5*(RRR(i,1)^2+RRR(i,2)^2)+sin(RRR(i,1)^2+RRR(i,2)^2))/C(3);
& && && &&&RRR(i,4)=(((3*RRR(i,1)-2*RRR(i,2)+4)^2)/8+((RRR(i,1)-RRR(i,2)+1)^2)/27+15)/C(4);
& && && &&&RRR(i,5)=(1/(RRR(i,1)^2+RRR(i,2)^2+1)-1.1*exp(-(RRR(i,1)^2-(RRR(i,2)^2))))/C(5);
& && && &&&RRR(i,6)=abs(RRR(i,3))+abs(RRR(i,4))+abs(RRR(i,5));
& && && &&&RRR(i,7)=0;
& && &&&end
& && && & RRR=sortrows(RRR,6);
& && && & FF(RR(1),=RRR(1,;
& && && & FF(RR(2),=RRR(2,;
& && & end
& &for m=1:10
& && & if rand(1)&0.01
& && && & MM=randperm(10);
& && && & MMM=zeros(1,7);
& && && & MMM(1,=FF(MM(1),;
& && && & t=rand(1)&0.5;
& && && & if t==0
& && && && &[~,P]=max(FF(:,6));
& && && && &MMM(1,1)=FF(MM(1),1)+(FF(MM(1),1)-FF(P,1))*rand(1);
& && && && &MMM(1,2)=FF(MM(1),2)+(FF(MM(1),2)-FF(P,2))*rand(1);
& && && & else
& && && && & [~,P]=min(FF(:,6));
& && && && &MMM(1,1)=MMM(1,1)+(MMM(1,1)-FF(P,1))*rand(1);
& && && && &MMM(1,2)=MMM(1,2)+(MMM(1,2)-FF(P,2))*rand(1);
& && && & end
& && && &&&MMM(1,3)=(0.5*(MMM(1,1)^2+MMM(1,2)^2)+sin(MMM(1,1)^2+MMM(1,2)^2))/C(3);
& && && &&&MMM(1,4)=(((3*MMM(1,1)-2*MMM(1,2)+4)^2)/8+((MMM(1,1)-MMM(1,2)+1)^2)/27+15)/C(4);
& && && &&&MMM(1,5)=(1/(MMM(1,1)^2+MMM(1,2)^2+1)-1.1*exp(-(MMM(1,1)^2-(MMM(1,2)^2))))/C(5);
& && && &&&MMM(1,6)=abs(MMM(1,3))+abs(MMM(1,4))+abs(MMM(1,5));
& && && &&&MMM(1,7)=0;
& && && &&&FF(MM(1),=MMM(1,;
& && & end
& &t=randperm(10);
& &t(2)=t(1)+4;
& &%D=zeros(1:CC-1,;
& &D=[FF(:,1:6); F(t(1):t(2),1:6)];
& &[E,P]=min(D(:,6));
& & a=D(P,3);
& & b=D(P,4);
& & c=D(P,5);
& &F(j,=D(P,;
& &m=size(D,1);
& &count=0;
& &K=zeros(1,m);
& &KKK=[];
& &t=1;& && &
& &gen=gen+1;& &
&&Y1=FF(:,3).*C(3);
&&Y2=FF(:,4).*C(4);
&&Y3=FF(:,5).*C(5);
&&Y=[Y1 Y2 Y3];
&&plot3(Y1,Y2,Y3,'*');
&&xlim([0 10]);
&&ylim([0 60]);
&&zlim([-0.1 0.3])
引用回帖:: Originally posted by dnp at
把程序贴出来,然后把错误的所有信息都贴出来,报错错误信息,错误发生的地方,错误的来源等,这样别人才好帮你排除问题,你提供的信息相当于没有任何信息~~ 代码贴出来了,错误信息是第77行矩阵赋值出现错误,提示是不合理的空矩阵安排,谢谢!
引用回帖:: Originally posted by dnp at
把程序贴出来,然后把错误的所有信息都贴出来,报错错误信息,错误发生的地方,错误的来源等,这样别人才好帮你排除问题,你提供的信息相当于没有任何信息~~ 代码已经贴出来了,错误信息是77行矩阵赋值出错,提示是不合适的空矩阵安排,错误来源我也不清楚为什么出现这种状况,我感觉是正确的,也可能是由于两个都是动态矩阵的缘故。现在不知道怎么样来解决这个问题。。
我跟踪了一下你矩阵D的大小,发现你的算法里面矩阵D是不断变小,而且会最终变成一个空的矩阵,自然在用矩阵D里面的元素对F矩阵进行赋值的时候会出错,所以你还是改进一下你的算法,并非matlab或者其他的问题。按照你的理解,可能矩阵在进行赋值之后,删除里面的元素的时候(命令D(t:m,=[],不会变成空的矩阵,但在运行的时候,这句话在循环体里面会使矩阵D成为空矩阵。这可能是你编地过程中有问题,或者逻辑上需要改进。
下面是我跟踪你不断用while循环过程中矩阵D大小的变化,到出错那一行的时候矩阵D是0乘6的矩阵,是空的,也就是说那次循环后D(t:m,=[];把矩阵D变成空的了,我不懂你的算法,所以你还是仔细考察改进一下比较好:CODE:The size of matrix D is 187 by 6 when gen = 0 and after iterating 1 times in the inner while loop.
The size of matrix D is 184 by 6 when gen = 0 and after iterating 2 times in the inner while loop.
The size of matrix D is 181 by 6 when gen = 0 and after iterating 3 times in the inner while loop.
The size of matrix D is 179 by 6 when gen = 0 and after iterating 4 times in the inner while loop.
The size of matrix D is 177 by 6 when gen = 0 and after iterating 5 times in the inner while loop.
The size of matrix D is 173 by 6 when gen = 0 and after iterating 6 times in the inner while loop.
The size of matrix D is 126 by 6 when gen = 0 and after iterating 7 times in the inner while loop.
The size of matrix D is 114 by 6 when gen = 0 and after iterating 8 times in the inner while loop.
The size of matrix D is 97 by 6 when gen = 0 and after iterating 9 times in the inner while loop.
The size of matrix D is 86 by 6 when gen = 0 and after iterating 10 times in the inner while loop.
The size of matrix D is 82 by 6 when gen = 0 and after iterating 11 times in the inner while loop.
The size of matrix D is 75 by 6 when gen = 0 and after iterating 12 times in the inner while loop.
The size of matrix D is 74 by 6 when gen = 0 and after iterating 13 times in the inner while loop.
The size of matrix D is 64 by 6 when gen = 0 and after iterating 14 times in the inner while loop.
The size of matrix D is 53 by 6 when gen = 0 and after iterating 15 times in the inner while loop.
The size of matrix D is 48 by 6 when gen = 0 and after iterating 16 times in the inner while loop.
The size of matrix D is 44 by 6 when gen = 0 and after iterating 17 times in the inner while loop.
The size of matrix D is 40 by 6 when gen = 0 and after iterating 18 times in the inner while loop.
The size of matrix D is 34 by 6 when gen = 0 and after iterating 19 times in the inner while loop.
The size of matrix D is 33 by 6 when gen = 0 and after iterating 20 times in the inner while loop.
The size of matrix D is 26 by 6 when gen = 0 and after iterating 21 times in the inner while loop.
The size of matrix D is 25 by 6 when gen = 0 and after iterating 22 times in the inner while loop.
The size of matrix D is 24 by 6 when gen = 0 and after iterating 23 times in the inner while loop.
The size of matrix D is 23 by 6 when gen = 0 and after iterating 24 times in the inner while loop.
The size of matrix D is 22 by 6 when gen = 0 and after iterating 25 times in the inner while loop.
The size of matrix D is 18 by 6 when gen = 0 and after iterating 26 times in the inner while loop.
The size of matrix D is 11 by 6 when gen = 0 and after iterating 27 times in the inner while loop.
The size of matrix D is 10 by 6 when gen = 0 and after iterating 28 times in the inner while loop.
The size of matrix D is 8 by 6 when gen = 0 and after iterating 29 times in the inner while loop.
The size of matrix D is 7 by 6 when gen = 0 and after iterating 30 times in the inner while loop.
The size of matrix D is 5 by 6 when gen = 0 and after iterating 31 times in the inner while loop.
The size of matrix D is 4 by 6 when gen = 0 and after iterating 32 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 0 and after iterating 33 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 0 and after iterating 34 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 0 and after iterating 35 times in the inner while loop.
The size of matrix D is 103 by 6 when gen = 1 and after iterating 1 times in the inner while loop.
The size of matrix D is 102 by 6 when gen = 1 and after iterating 2 times in the inner while loop.
The size of matrix D is 97 by 6 when gen = 1 and after iterating 3 times in the inner while loop.
The size of matrix D is 96 by 6 when gen = 1 and after iterating 4 times in the inner while loop.
The size of matrix D is 91 by 6 when gen = 1 and after iterating 5 times in the inner while loop.
The size of matrix D is 90 by 6 when gen = 1 and after iterating 6 times in the inner while loop.
The size of matrix D is 87 by 6 when gen = 1 and after iterating 7 times in the inner while loop.
The size of matrix D is 86 by 6 when gen = 1 and after iterating 8 times in the inner while loop.
The size of matrix D is 72 by 6 when gen = 1 and after iterating 9 times in the inner while loop.
The size of matrix D is 25 by 6 when gen = 1 and after iterating 10 times in the inner while loop.
The size of matrix D is 13 by 6 when gen = 1 and after iterating 11 times in the inner while loop.
The size of matrix D is 5 by 6 when gen = 1 and after iterating 12 times in the inner while loop.
The size of matrix D is 4 by 6 when gen = 1 and after iterating 13 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 1 and after iterating 14 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 1 and after iterating 15 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 1 and after iterating 16 times in the inner while loop.
The size of matrix D is 90 by 6 when gen = 2 and after iterating 1 times in the inner while loop.
The size of matrix D is 89 by 6 when gen = 2 and after iterating 2 times in the inner while loop.
The size of matrix D is 88 by 6 when gen = 2 and after iterating 3 times in the inner while loop.
The size of matrix D is 83 by 6 when gen = 2 and after iterating 4 times in the inner while loop.
The size of matrix D is 82 by 6 when gen = 2 and after iterating 5 times in the inner while loop.
The size of matrix D is 77 by 6 when gen = 2 and after iterating 6 times in the inner while loop.
The size of matrix D is 76 by 6 when gen = 2 and after iterating 7 times in the inner while loop.
The size of matrix D is 73 by 6 when gen = 2 and after iterating 8 times in the inner while loop.
The size of matrix D is 72 by 6 when gen = 2 and after iterating 9 times in the inner while loop.
The size of matrix D is 56 by 6 when gen = 2 and after iterating 10 times in the inner while loop.
The size of matrix D is 40 by 6 when gen = 2 and after iterating 11 times in the inner while loop.
The size of matrix D is 8 by 6 when gen = 2 and after iterating 12 times in the inner while loop.
The size of matrix D is 5 by 6 when gen = 2 and after iterating 13 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 2 and after iterating 14 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 2 and after iterating 15 times in the inner while loop.
The size of matrix D is 51 by 6 when gen = 3 and after iterating 1 times in the inner while loop.
The size of matrix D is 50 by 6 when gen = 3 and after iterating 2 times in the inner while loop.
The size of matrix D is 49 by 6 when gen = 3 and after iterating 3 times in the inner while loop.
The size of matrix D is 48 by 6 when gen = 3 and after iterating 4 times in the inner while loop.
The size of matrix D is 42 by 6 when gen = 3 and after iterating 5 times in the inner while loop.
The size of matrix D is 41 by 6 when gen = 3 and after iterating 6 times in the inner while loop.
The size of matrix D is 36 by 6 when gen = 3 and after iterating 7 times in the inner while loop.
The size of matrix D is 35 by 6 when gen = 3 and after iterating 8 times in the inner while loop.
The size of matrix D is 34 by 6 when gen = 3 and after iterating 9 times in the inner while loop.
The size of matrix D is 31 by 6 when gen = 3 and after iterating 10 times in the inner while loop.
The size of matrix D is 30 by 6 when gen = 3 and after iterating 11 times in the inner while loop.
The size of matrix D is 14 by 6 when gen = 3 and after iterating 12 times in the inner while loop.
The size of matrix D is 4 by 6 when gen = 3 and after iterating 13 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 3 and after iterating 14 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 3 and after iterating 15 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 3 and after iterating 16 times in the inner while loop.
The size of matrix D is 53 by 6 when gen = 4 and after iterating 1 times in the inner while loop.
The size of matrix D is 52 by 6 when gen = 4 and after iterating 2 times in the inner while loop.
The size of matrix D is 51 by 6 when gen = 4 and after iterating 3 times in the inner while loop.
The size of matrix D is 50 by 6 when gen = 4 and after iterating 4 times in the inner while loop.
The size of matrix D is 44 by 6 when gen = 4 and after iterating 5 times in the inner while loop.
The size of matrix D is 43 by 6 when gen = 4 and after iterating 6 times in the inner while loop.
The size of matrix D is 38 by 6 when gen = 4 and after iterating 7 times in the inner while loop.
The size of matrix D is 37 by 6 when gen = 4 and after iterating 8 times in the inner while loop.
The size of matrix D is 36 by 6 when gen = 4 and after iterating 9 times in the inner while loop.
The size of matrix D is 33 by 6 when gen = 4 and after iterating 10 times in the inner while loop.
The size of matrix D is 32 by 6 when gen = 4 and after iterating 11 times in the inner while loop.
The size of matrix D is 16 by 6 when gen = 4 and after iterating 12 times in the inner while loop.
The size of matrix D is 7 by 6 when gen = 4 and after iterating 13 times in the inner while loop.
The size of matrix D is 6 by 6 when gen = 4 and after iterating 14 times in the inner while loop.
The size of matrix D is 5 by 6 when gen = 4 and after iterating 15 times in the inner while loop.
The size of matrix D is 4 by 6 when gen = 4 and after iterating 16 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 4 and after iterating 17 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 4 and after iterating 18 times in the inner while loop.
The size of matrix D is 12 by 6 when gen = 5 and after iterating 1 times in the inner while loop.
The size of matrix D is 11 by 6 when gen = 5 and after iterating 2 times in the inner while loop.
The size of matrix D is 10 by 6 when gen = 5 and after iterating 3 times in the inner while loop.
The size of matrix D is 7 by 6 when gen = 5 and after iterating 4 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 5 and after iterating 5 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 5 and after iterating 6 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 5 and after iterating 7 times in the inner while loop.
The size of matrix D is 4 by 6 when gen = 6 and after iterating 1 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 6 and after iterating 2 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 6 and after iterating 3 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 6 and after iterating 4 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 7 and after iterating 1 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 7 and after iterating 2 times in the inner while loop.
The size of matrix D is 1 by 6 when gen = 7 and after iterating 3 times in the inner while loop.
The size of matrix D is 3 by 6 when gen = 8 and after iterating 1 times in the inner while loop.
The size of matrix D is 2 by 6 when gen = 8 and after iterating 2 times in the inner while loop.
The size of matrix D is 0 by 6 when gen = 8 and after iterating 3 times in the inner while loop.
??? Improper assignment with rectangular empty matrix.,
引用回帖:: Originally posted by dnp at
我跟踪了一下你矩阵D的大小,发现你的算法里面矩阵D是不断变小,而且会最终变成一个空的矩阵,自然在用矩阵D里面的元素对F矩阵进行赋值的时候会出错,所以你还是改进一下你的算法,并非matlab或者其他的问题。按照你 ... 有表情的那个命令是:CODE:D(t:m,=[];建议最好还是不要用这个方式来改变D矩阵,直接用索引的方法来提取D矩阵里面的元素不论从效率还是理解的角度讲更好~~
24小时热帖
下载小木虫APP
与700万科研达人随时交流coding change world!
利用Matlab 解决二维矩阵问题
Matlab是一款非常强大的数学计算工具,学习并使用它进行处理一些数据运算,将会非常之高效。
今天有同学问我了一道关于利用Matlab 解决二维矩阵问题,利用空闲时间给他解答,希望能帮助到他。
1,问题描述
创建一个矩阵a,其内容为50行50列随机矩阵;
用循环语句求矩阵内元素的最大值;
用循环语句求矩阵内元素的最小值;
用循环语句求矩阵内元素的所有元素和;
用循环语句将对角线元素显示出来;
2,问题分析
Matlab中有很多函数库,它们可以帮助我们解决很多函数问题,因此在解决问题时,调用它们,会大大简便我们的操作。
针对这个问题,我第一个想法就是利用Matlab自带的函数去求解,得到对应答案。
如果同学还需要自己建函数来表示,只需要对原库函数进行分析即可。
3,解决问题
创建一个矩阵a,其内容为50行50列随机矩阵;
rand(m)是生成m*m,0到1之间的随机数,但由于是生成的为小数,所以我将范围扩大100倍,此时就出现了下面代码:
A=round(100*rand(50))
结果部分展示:
因为生成50*50的矩阵,截图页面展示不下,只显示部分截图。
2.求矩阵内元素的最大值,最小值,求和;
可以利用max()来解决,max(A),代表A中每列的最大值,max(max(A))就可以代表,整个矩阵的最大值。
代码如下:
max(max(A))
结果展示:
同理最小值代码
min(min(A))
给矩阵求和亦是如此
Sum=sum(sum(A))
3.求矩阵内元素的平均值;
a=mean(A,1) %按列平均
b=mean(A,2) %按行平均
c=mean(A(:)) %全部平均
结果显示:
4.将对角线元素显示出来
在matlab中也有一个函数专门解决对角线元素的输出。
D = diag( A )
再将结果转置
至此上面的问题全部解决。
至于,通过循环来些函数实现,因为时间的问题,我不能一一列出,但是你可以通过输入help f(x) 来具体查询函数的功能和定义,然后逆推写出程序。
方法如下:
因为同学需要帮助,我花半中午时间来梳理,写作,希望能够帮助到你。
如果能帮助到CSDN上的同学,更是荣幸。
作者:新海
写于12.1日
分享也是一种成长。
没有更多推荐了,
加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!}

我要回帖

更多关于 python 二维矩阵画图 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信