# Reshape data vector for a surf plot over a non-rectangular domain

2 views (last 30 days)
David Hoffmeyer on 25 Nov 2020
Commented: David Hoffmeyer on 25 Nov 2020
I have a number of points in the x-y-plane defined by the vectors X and Y, and associated functions values for those points in the vector Z. I need to make a surf plot of the data in Z over the domain defined by X and Y. The values in Z cannot be evaluated from an analytical expression, thus I cannot use meshgrid in the traditional way. How do I reshape the data in Z in order to fit the required input of the surf function?
Also, I would like the surf plot to only cover the geomtry in the x-y-plane defined by X and Y. I have attached a patch plot of the data over a diamond-shaped geometry. How do I ensure that the surf plot is generated over the diamond-shaped domain only?

Bjorn Gustavsson on 25 Nov 2020
If you have your x, y and z data in 1-D arrays you can use trisurf see the help and documentation for that function for usage. If you want this type of projection just set the view to be from above after the call to trisurf: view(0,90).
HTH

David Hoffmeyer on 25 Nov 2020
Thank you, Bjorn. Though the edges are not quite as sharp as desired due to the triangulation used by trisurf.
Bjorn Gustavsson on 25 Nov 2020
For that, it depends on how you want the plot to look, you might get away with cleaning away the large blue and light-blue triangles by maually removing them from the triangulation - I've never bothered doing that kind of thing, so have no solutions except suggesting maual inspection of the tri structure.

Bruno Luong on 25 Nov 2020
Edited: Bruno Luong on 25 Nov 2020
A diamond shape is bijective mapped from/to a rectangular shape, thus meshgrid/ndgrid combined with surf are still applicable.
n = 100;
s=linspace(0,1,n);
t=linspace(0,1,n);
[s,t] = meshgrid(s,t);
[sc,tc] = meshgrid([0 1],[0 1]);
x = interp2(sc,tc,[0 4; -4 0], s, t);
y = interp2(sc,tc,[-2 0; 0 5], s, t);
z = peaks(n); % scattered interpolation with nearest extrapolation from your X,Y,Z data is needed to get z
surf(x,y,z,'EdgeColor','none');
view(2)
axis equal
colormap(jet)
The advantage compared to trisurf proposed by Bjorn is you can make sure the boundary of the shape are straight.

David Hoffmeyer on 25 Nov 2020
Bruno, can you give a hint on how to perform the scattering interpolation of the data in Z?
Also, it seems that you define the shape of the diamond in the interp2 function. Can you method work for an arbitrary geometry given by the vectors X and Y?
Bruno Luong on 25 Nov 2020
F = scatteredInterpolant(X,Y,Z,'linear','nearest');
z = F(x,y);
David Hoffmeyer on 25 Nov 2020
Something is not right with the scatteredInterpolant function. Also, As far as I can see, you are manually defining the diamond geometry, so can this work on arbitrary geometries?

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!