我在嵌套for循环中生成的矩阵中有数据。我想只绘制满足特定条件的数据(例如,它必须大于0.6
)。数据点是否符合该条件在1
矩阵中存储为0
或mlist
。
在Matlab中绘制此图的最简单方法是什么?对于不符合条件的数据点,它可以只是空格。
xlist = linspace(-1,1,20);
ylist = linspace(-2,2,30);
zlist = zeros(length(xlist),length(ylist));
mlist = zeros(length(xlist),length(ylist));
% iteration counter
ii = 0;
jj = 0;
for x = xlist
ii = ii + 1;
for y = ylist
z = sin(x*y);
jj = jj + 1;
zlist(jj) = z;
if z > 0.6
mlist(jj) = 1;
else
mlist(jj) = 0;
end
end
end
contourf(ylist,xlist,zlist)
mesh(ylist,xlist,zlist)
答案 0 :(得分:1)
在MATLAB中“删除”数据时,无需从数据中实际删除数据(因为您将遇到非均匀网格等问题)正在将其替换为NaN
,如大多数MATLAB图将NaN
视为缺失数据,并且不会在该点上绘制任何内容。
您可以将程序更改为不使用循环:
[x,y]=meshgrid(linspace(-1,1,20),linspace(-2,2,30));
z = sin(x.*y);
zlist(z<0.6)=NaN;
contourf(y,x,zlist); % // maybe x,y?
答案 1 :(得分:0)
我认为,最简单的方法是限制z轴:
xlist = linspace(-1,1,20);
ylist = linspace(-2,2,30);
zlist = zeros(length(xlist),length(ylist));
mlist = zeros(length(xlist),length(ylist));
% iteration counter
ii = 0;
jj = 0;
for x = xlist
ii = ii + 1;
for y = ylist
z = sin(x*y);
jj = jj + 1;
zlist(jj) = z;
end
end
contourf(ylist,xlist,zlist)
mesh(ylist,xlist,zlist)
zlim([0.6,max(z)])
为了增加可见性,我会考虑将contourf和mesh调用更改为:
surf(ylist,xlist,zlist)