MATLAB: Make matrix from two variables and plot contour from third variable
I have 3 variables (x
, y
, z
) each in a 192x1 vector. The data is all random and sometimes has missing values (NaNs).
I would like to plot variable c
against the other two variables x
and y
as contour plot (x
on the x-axis, y
on the y-开发者_StackOverflowaxis, and z
making up the contour lines).
My biggest problem is making up the 192x192 matrix for x
and y
because x
and y
consist of random data. I have tried to use GRIDDATA, Delaunay, and TriScatteredInterp but they did not work because I have NaNs in my data.
Does anyone know what I can do? If there was a way that I could return the matrix of x
vs y
from the scatter plot of x
and y
then my problem would be solved.
Example code
x=rand(192,1);
y=rand(192,1);
z=rand(192,1);
[X,Y]=meshgrid(x,y);
contour(X,Y,z);
??? Error using ==> contour at 74 Z must be size 2x2 or greater.
What are you going to do with NaNs? Do they contain any additional information to draw the contour plot? Cannot you just remove them?
idx = any(isnan([x y z]),2);
x(idx)=[];
y(idx)=[];
z(idx)=[];
Then you can apply solution from this question. The missing data will be interpreted anyway.
Because of the ature of contour plots, the problem is with size of the matrix z. If you debug your code you see that after calling meshgrid, X and Y become of size 192 x 192. Simply change the following line and it will work! Let me know if the problem was solved ;)
z=rand(192,192);
精彩评论