My matlab code with interp3 is not providing me correct answer. Please help.

1 view (last 30 days)
I have a data (shown on left). I have prepared a matlab code (shown on right). The output should have come 13 for (x,y,z)=(1,2,3) but I am getting the value 18. Please show where should I do the necessary correction.

Accepted Answer

Stephen23
Stephen23 on 12 Sep 2022
Edited: Stephen23 on 12 Sep 2022
format compact
T = readtable('interpcheck.csv')
T = 24×5 table
index x y z result _____ _ _ _ ______ 1 1 1 1 2 2 1 1 2 4 3 1 1 3 6 4 1 1 4 8 5 1 2 1 7 6 1 2 2 10 7 1 2 3 13 8 1 2 4 16 9 1 3 1 12 10 1 3 2 16 11 1 3 3 20 12 1 3 4 24 13 2 1 1 15 14 2 1 2 18 15 2 1 3 21 16 2 1 4 24
A = accumarray([T.x,T.y,T.z],T.result)
A =
A(:,:,1) = 2 7 12 15 21 27 A(:,:,2) = 4 10 16 18 26 34 A(:,:,3) = 6 13 20 21 31 41 A(:,:,4) = 8 16 24 24 36 48
X = unique(T.x);
Y = unique(T.y);
Z = unique(T.z);
interpn(X,Y,Z,A,1,2,3)
ans = 13
But using GRIDDATA is simpler:
griddata(T.x,T.y,T.z,T.result,1,2,3)
ans = 13

More Answers (1)

Cris LaPierre
Cris LaPierre on 12 Sep 2022
Edited: Cris LaPierre on 12 Sep 2022
I suggest looking at the description of what the inputs to interp3 can be.
By taking the unique value of X, Y, and Z, you are losing the relationship between the values. Since you have all the data, I'd be inclided to use reshape to have the inputs be 3D arrays. When doing this, note that rows correspond to y, and columns to x. That means needing to sort your data so that the output of reshape correctly organizes the data.
file = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1123055/interpcheck.csv';
data = readtable(file);
data = sortrows(data,["z","x","y"])
data = 24×5 table
index x y z result _____ _ _ _ ______ 1 1 1 1 2 5 1 2 1 7 9 1 3 1 12 13 2 1 1 15 17 2 2 1 21 21 2 3 1 27 2 1 1 2 4 6 1 2 2 10 10 1 3 2 16 14 2 1 2 18 18 2 2 2 26 22 2 3 2 34 3 1 1 3 6 7 1 2 3 13 11 1 3 3 20 15 2 1 3 21
x=reshape(data.x,[3,2,4]);
y=reshape(data.y,[3,2,4]);
z=reshape(data.z,[3,2,4]);
V = reshape(data.result,[3,2,4]);
r = interp3(x,y,z,V,1,2,3)
r = 13
Your example data set does allow you to use use unique here. You do have to keep in mind that the size of v must be [length(Y) length(X) length(Z)]. However, you can't just arbitrarily make it the size you want. That will cause a disconnect between your x,y,z values and the corresponding result value. This is why you are getting 18. You must either sort the data so that the output of reshape treats the rows as y and columns as x
data = sortrows(data,["z","x","y"])
x = unique(data.x);
y = unique(data.y);
z = unique(data.z);
V = reshape(data.result,3,2,4);
r = interp3(x,y,z,V,1,2,3)
or reshape the data to match the format in the table, and then permute to get it to be what is needed.
x = unique(data.x);
y = unique(data.y);
z = unique(data.z);
V = reshape(data.result,4,3,2);
v = permute(V,[2,3,1]);
r = interp3(x,y,z,v,1,2,3)
  3 Comments
Torsten
Torsten on 12 Sep 2022
Edited: Torsten on 12 Sep 2022
But griddata usually is for scattered data ... So equivalent to scatteredInterpolant, I guess.
It should be possible to indicate that the data stem from a regular grid, but one doesn't need to care about the ordering.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by