0

I have data in a matrix generated in a nested for loop. I want to only plot the data that meets a certain condition (e.g. it must be bigger than 0.6). Whether or not the data point meets that condition is stored as 1 or 0 in my mlist matrix.

What is the easiest way to plot this in Matlab? For the data points that don't meet the condition, it can just be white space.

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)
Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75

2 Answers2

1

On way of "removing" data in plots in MATLAB without needing to actually remove it from your data (as you'll have problems with non-uniform grids and so on) is replacing it by NaN, as most of the MATLAB plots will treat a NaN as missing data and will not draw anything on that point.

You can change your program to not use loops as:

[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?
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
0

The easiest way, I believe, would be to limit the z-axis:

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)])

And for added visibility, I would consider changing the contourf and mesh calls to:

surf(ylist,xlist,zlist)
jkazan
  • 1,149
  • 12
  • 29