Reputation: 571
I've a matrix M
10201x3 where the first 2 column are constant used to compute the response Z
in column 3. Example:
.... ... .................
0.0031 0.02 0.792729854583740
0.0031 0.03 0.802729845046997
0.0031 0.04 0.812729895114899
0.0031 0.05 0.822729885578156
.... ... .................
0.0034 0.02 0.867461800575256
0.0034 0.03 0.877461791038513
0.0034 0.04 0.887461841106415
0.0034 0.05 0.897461831569672
0.0034 0.06 0.907461822032929
.... ... .................
I wanna make a contour plot where X = M(:,1)
, Y = M(:,2)
and Z = M(:,3)
with different colors for different hights. I need to do the same thing in both 2D and 3D.
Upvotes: 3
Views: 4710
Reputation: 3832
I would suggest to transform this array to three 2D arrays using interpolation with function griddata()
. Interpolation could be useful for nonregular data. First, we create grid of coordinates:
xq=min(X):(max(X)-min(X))/200:max(X);
yq=min(Y):(max(Y)-min(Y))/200:max(Y);
[Xq, Yq] = meshgrid(xq,yq);
Than, we use interpolating:
Zq =griddata(X,Y,Z,Xq,Yq);
Than you can plot:
countour(Xq,Yq,Zq)
Upvotes: 1
Reputation: 25232
I assume your data is regular, and you know how many repeating x
-elements you have.
Let's call the number of repeating x = L
- or you'll be able to find that out.
You need to reshape your vectors:
X = reshape(X,[],L);
Y = reshape(Y,[],L);
Z = reshape(Z,[],L);
You need Z
how it is, but just the first row of X
and the first column of Y
.
X = X(:,1);
Y = Y(1,:);
and then you can use contour
:
contour(X,Y,Z);
There is no need for interpolation!
contour(X,Y,Z), contour(X,Y,Z,n), and contour(X,Y,Z,v) draw contour plots of Z using X and Y to determine the x- and y-axis limits.
If X and Y are vectors, then the length of X must equal the number of columns in Z and the length of Y must equal the number of rows in Z.
If X and Y are matrices, then their sizes must equal the size of Z.
Therefore shorter:
X = X(1:L:end);
Y = Y(1:L);
Z = reshape(Z,[],L);
contour(X,Y,Z);
Upvotes: 4