Finding the Meshgrid provided a surface.

Hi All, given a surface, is there anyway to find the underlying meshgrid of the surface. For example, I have a surface given by function Z that has been created in the following manner:
x = -4:0.4:4; y = -8:0.4:8; [X,Y] = meshgrid(x,y);
Z = comp_z(X,Y);
%Given just the surface, Z , is there a way to find the meshgrid that created it?

1 Comment

"Given just the surface, Z , is there a way to find the meshgrid that created it?"
In general this is not possible. Consider a simple 2D example: if I give you:
Y = [0,-0.70711,-1,-0.70711,0,0.70711,1,0.70711,0]
can you tell me which X values I used the generate those Y values?:
Y = sin(X)
Unless you have other information (e.g. about the shape of the curve, sample spacing, a range, a domain and some other restrictions, etc) then the general answer is this is not possible.

Sign in to comment.

 Accepted Answer

How are you given the surface? As a fig file? You could extract the X and Y indices of the surface:
x = -4:0.4:4;
y = -8:0.4:8;
[X,Y] = meshgrid(x,y);
Z = (X.*Y);
surf(X,Y,Z)
h = findobj('Type','Surface');
surfX = h.XData;
surfY = h.YData;
all(all(surfX==X))
ans =
1
all(all(surfY==Y))
ans =
1

5 Comments

Hi Cris, thanks for your assistance,
Basically, its part if a bigger function, I want to be able to input a surface,Z ( a function of x and y), into the function and be able to interpolate values of Z (using cubic interpolation) at different points. This is what I have so far:
function [z0] = myinterp_function(Z,x0,y0)
X =?
Y = ?
fun_interp = griddedInterpolant(X',Y',Z','cubic');
z0 = fun_interp(x0,y0);
end
Essentialy I want Z to be defined outside of the function but for this to work I will need a way to get X and Y from Z.
So it's not a surface in MATLAB yet. You have a matrix of numbers and want to somehow reverse engineer it to get what the values of X and Y were to compute the Z values?
Can you not also pass in your X and Y values?
The only other option I can think of is to use your equation and the z values you have. Create a defined interpolated grid of x values and solve for the corresponding y values.
Yes exactly, I just want to pass Z in and retrieve X and Y inside the function so I can perform the interpolation.
No I need to define Z outside of the function.
The Z function I have is: z = cos(x/2).* cos(y) + (y/10) - (x/5);
I didn't say define Z inside the function. I said use the values of Z and your interpolated X to solve for Y. You might be able to use the solve function in MATLAB
I've had a look and a think and I don't think what I want is possible.
Thankyou regardless.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!