How can I look up and interpolate a value from a set of 3D gridded data? I am given Y and Z dimensions, and need to find X.

10 views (last 30 days)
I am trying to look up and interpolate to the nearest X value from a set of gridded data. This data is a formatted so that X is an 12x1 column vector, Y is a 17x1 column vector, and Z is a 12x17 matrix.
Here is an example data set:
X =[11;10;9;8;7;6;5;4;3;2;1;0]
Y=[0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16]
Z=[ 3.50 3.32 3.12 2.89 2.63 2.30 1.91 1.51 1.18 0.88 0.59 0.29;
3.29 3.08 2.85 2.59 2.29 1.95 1.58 1.22 0.92 0.68 0.45 0.22;
3.35 3.12 2.87 2.58 2.26 1.92 1.57 1.21 0.90 0.65 0.43 0.22;
3.41 3.17 2.90 2.60 2.29 1.95 1.62 1.28 0.95 0.68 0.45 0.23;
3.45 3.20 2.93 2.64 2.34 2.02 1.70 1.37 1.03 0.74 0.49 0.24;
3.47 3.22 2.96 2.69 2.40 2.10 1.78 1.46 1.12 0.80 0.53 0.26;
3.48 3.25 3.00 2.74 2.46 2.17 1.87 1.54 1.21 0.87 0.57 0.28;
3.50 3.27 3.03 2.78 2.52 2.24 1.95 1.63 1.29 0.94 0.61 0.31;
3.50 3.29 3.06 2.83 2.57 2.31 2.02 1.70 1.37 1.01 0.66 0.33;
3.50 3.30 3.08 2.86 2.62 2.36 2.08 1.78 1.44 1.08 0.71 0.35;
3.49 3.29 3.09 2.87 2.64 2.40 2.13 1.84 1.51 1.15 0.76 0.38;
3.45 3.27 3.07 2.87 2.65 2.42 2.17 1.89 1.57 1.21 0.81 0.41;
3.37 3.20 3.02 2.83 2.63 2.41 2.17 1.91 1.62 1.27 0.87 0.43;
3.09 2.93 2.77 2.59 2.40 2.20 1.98 1.74 1.47 1.15 0.77 0.39;
2.47 2.33 2.17 2.01 1.85 1.67 1.47 1.26 1.03 0.78 0.52 0.26;
1.90 1.78 1.65 1.51 1.38 1.23 1.07 0.91 0.73 0.55 0.37 0.18;
1.47 1.37 1.27 1.16 1.05 0.93 0.81 0.68 0.54 0.41 0.27 0.14]
I would like to be able to take a Y and Z value, and look up an X value.
I've tried griddedInterpolant, but was not able figure out the function.
Any help appreciated

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 7 Aug 2015
F = griddedInterpolant({Y,flip(X)},flip(Z,2));
z = [3;2;1];
y = [0;8;3];
your_x = arrayfun(@(y,z)fzero(@(x)z - F(y,x),0),y,z);

More Answers (1)

Walter Roberson
Walter Roberson on 7 Aug 2015
  2 Comments
John Bulzacchelli
John Bulzacchelli on 7 Aug 2015
Edited: John Bulzacchelli on 7 Aug 2015
Thanks, Walter. I'm struggling to even get your example function to work - it returns the error for line 10 that my input arguments must be double.
I tried entering in my arguments as: X=double(52) Z=double(10) Then calling the function as: Y=lookup(X,Z)
Also as:
XYZ = [0 40 50 60
0.70 22.70 9.24 6.78
0.65 20.45 8.85 6.42
0.60 19.23 8.48 6.69
0.55 18.04 8.03 6.28
0.50 16.89 7.63 6.49];
[numrow, numcol] = size(XYZ);
xcol = interp(XYZ(1,:), 1:numcol, double(52), 'nearest');
yrow = interp1(XYZ(:, xcol), 1:numrow, double(10), 'nearest');
Y = XYZ(yrow, 1);
Walter Roberson
Walter Roberson on 7 Aug 2015
I corrected some errors in that routine just now.
In terms of your existing variables:
[numrow, numcol] = size(Z);
xcol = interp1(X, 1:numcol, X_to_search, 'nearest', 'extrap');
ycol = interp1(Z(:,xcol), 1:numrow, Z_to_search, 'nearest', 'extrap');
Y = Y(ycol);

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!