在Matlab中替换网格中的NAN区域

时间:2017-07-14 09:09:07

标签: matlab matlab-figure

我在下面写了代码来显示一个矢量字段:

clear all;
close all;
phi = 90;
[x,y] = meshgrid(-3:0.1:3,-3:0.1:3);
u = (x.*(-1+3.*(x.*cosd(phi)+y.*sind(phi)).^2./(x.^2+y.^2))./(x.^2+y.^2).^(3/2))+...
    ((x-2).*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
    ((x+2).*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
v = (y.*(-1+3.*(x.*cosd(phi)+y.*sind(phi)).^2./(x.^2+y.^2))./(x.^2+y.^2).^(3/2))+...
    (y.*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
    (y.*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
h = streamslice(x,y,u,v,0.5);

问题是,空间中有三个点uv变为无限:(y=0,x=0)(y=0,x=-2)(y=0,x=2)。当vu变为无限时,该区域中没有向量字段,输出为空区域。我希望MATLAB省略vu的无限部分,并绘制vu的其他部分。例如,在(y=0,x=0),我想在下面显示向量字段(这不是无限的):

u = ((x-2).*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
    ((x+2).*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));
v = (y.*(-1+3.*((x-2).*cosd(phi)+y.*sind(phi)).^2./((x-2).^2+y.^2))./((x-2).^2+y.^2).^(3/2))+...
    (y.*(-1+3.*((x+2).*cosd(phi)+y.*sind(phi)).^2./((x+2).^2+y.^2))./((x+2).^2+y.^2).^(3/2));

1 个答案:

答案 0 :(得分:0)

您可以创建索引以仅替换非有限值:

u = [1 1 inf NaN 1 1];
v = [1 1 inf NaN 1 1];

u1 = [2 2 2 2 2 2];
v1 = [2 2 2 2 2 2];

%Now, if an element of u or v is not finite it is replaced by the same value of the matrices u1,v1.

u(~isfinite(u))=u1(~isfinite(u));
v(~isfinite(v))=v1(~isfinite(v));

第二个选项:

您还可以使用griddata

插入非有限值
finite     = isfinite(u) & isfinite(v);
u = griddata(x(finite), y(finite), u(finite), x, y);
v = griddata(x(finite), y(finite), v(finite), x, y);

<强> BEFORE:

enter image description here

<强> AFTER:

enter image description here