MATLAB: Calculation with Certain Portion of the Matrices

contourfor loopif statementmatrix manipulation

xm = 0:0.1:6; ym =3:0.1:9;
[x, y] = meshgrid(xm,ym);
angle = asind((-x.^2.*y.^2-y.^4+1296)./(2.*x.*y.^3));
angle = real(angle);
z2 = 6./(x.*sind(angle)+y);
figure
contourf(x,angle,z2,0.82:0.03:2.2,'ShowText','on');
For the above code, I want to calculate z2 for the matrix elements of angles having the values between (-40) to 20 degrees, other values must vanish from the matrix. I know for these purpose x and y matrices should match with the angle matrix and they also need to be arranged, but ı couldn't manage the issue.
As a note, limiting the plotting axis is not the point because I will use the data for another calculation, I want to clear out unwanted parts of the data.

Best Answer

The only way you're going to avoid that sawtooth effect is by starting with a meshgridded x and angle and calculating y from that. That would involve working out the equation of y against angle and x. If you can't do that, then you can reduce but not completely elimiate the sawtooth effect by using a finer grid.
I find it a bit weird that it is the angle matrix that you set to NaN. For contourf it doesn't matter but I would have thought that setting the z2 matrix to NaN would make more sense if that's the one you're going to use for further calculation.
Note that find is rarely needed and it's certainly not needed in the code you wrote:
indices = angle < -50;
angle(indices) = NaN;
indices = angle > 30;
angle(indices) = Nan;
would produce the same result. This can be simplified into a one-liner:
angle(angle < -50 | angle > 30) = NaN;
and as said, it's probably better to alter z2 instead of angle, so I'd replace that by:
z2(angle < -50 | angle > 30) = NaN;