Mikhail Genkin
Mikhail Genkin

Reputation: 3460

Converting matrix data representation into meshgrid

I calculated the function of interest $f(x,y)$ numerically in Mathematica. For consistency, I want to use Matlab for plotting.

I exported my function in the matrix form:

 test =

-1.0000   -1.0000   -0.4864
-1.0000   -0.6000   -0.2804
-1.0000   -0.2000   -0.0462
-1.0000    0.2000   -0.0462
-1.0000    0.6000   -0.2804
-1.0000    1.0000   -0.4864
-0.6000   -1.0000   -0.2997
-0.6000   -0.6000   -0.1526
-0.6000   -0.2000    0.1118
-0.6000    0.2000    0.1118
-0.6000    0.6000   -0.1526
-0.6000    1.0000   -0.2997
-0.2000   -1.0000   -0.1809
-0.2000   -0.6000   -0.0939
-0.2000   -0.2000   -0.0046
-0.2000    0.2000   -0.0046
-0.2000    0.6000   -0.0939
-0.2000    1.0000   -0.1809
 0.2000   -1.0000   -0.1809
 0.2000   -0.6000   -0.0939
 0.2000   -0.2000   -0.0046
 0.2000    0.2000   -0.0046
 0.2000    0.6000   -0.0939
 0.2000    1.0000   -0.1809
 0.6000   -1.0000   -0.2997
 0.6000   -0.6000   -0.1526
 0.6000   -0.2000    0.1118
 0.6000    0.2000    0.1118
 0.6000    0.6000   -0.1526
 0.6000    1.0000   -0.2997
 1.0000   -1.0000   -0.4864
 1.0000   -0.6000   -0.2804
 1.0000   -0.2000   -0.0462
 1.0000    0.2000   -0.0462
 1.0000    0.6000   -0.2804
 1.0000    1.0000   -0.4864

So that test is 36x3 matrix, where the columns are x, y and f(x,y). Now I need to contour plot it.

However, it is not in the form of meshgrid. Any thoughts, how to quickly convert it in the form that can be plotted by contour function?

Upvotes: 0

Views: 827

Answers (1)

Dohyun
Dohyun

Reputation: 642

If your data is regular (data set coincide with meshgrid but in vector form), then you can use reshape.

xgrid=6;
ygrid=6;
contour(reshape(test(:,1),xgrid,ygrid),reshape(test(:,2),xgrid,ygrid),reshape(test(:,3),xgrid,ygrid))

Another option is tricontour which will also work for irregular dataset

tri = delaunay(test(:,1),test(:,2));

figure;
tricontour(tri,test(:,1),test(:,2),test(:,3));

You can find tricontour at https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data/content/tricontour.m

If your surface is not convex,

then you need to remove some of triangulation from tri because of the nature of delaunay

Upvotes: 2

Related Questions