优化这段代码figure(2) vx=zeros(11,11); vy=zeros(11,11); for m=2:10 vx(:,m)=(u(:,m-1)-u(:,m+1)).*10; end for n=2:6 vy(n,:)=(u(n-1,:)-u(n+1,:)).*10; end vx(:,1)=(u(:,1)-u(:,2)).*20; vx(:,11)=(u(:,10)-u(:,11)).*20; vy(1,:)=(u(1,:)-u(2,:)).*20; vy(11,:)=(u(10,:)-u(11,:)).*20; quiver(x,y,vx,vy,1); axis([0,6,0,0.67]); title('电力线');
时间: 2023-05-16 13:04:57 浏览: 118
2.1图像目标边界描述.zip_4 3 2 1_ZEROS-7_图像目标边界描述
可以使用矩阵运算来优化这段代码,如下所示:
figure(2)
vx=zeros(11,11);
vy=zeros(11,11);
vx(:,2:10)=(u(:,1:9)-u(:,3:11)).*10;
vy(2:6,:)=(u(1:5,:)-u(3:7,:)).*10;
vx(:,1)=(u(:,1)-u(:,2)).*20;
vx(:,11)=(u(:,10)-u(:,11)).*20;
vy(1,:)=(u(1,:)-u(2,:)).*20;
vy(11,:)=(u(10,:)-u(11,:)).*20;
quiver(x,y,vx,vy,1);
axis([0,6,0,0.67]);
title('电力线');
这样可以避免使用循环,提高代码的效率。
阅读全文