1 ビュー (過去 30 日間)
Junaed Hossain 2022 年 9 月 12 日

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.
##### 2 件のコメント表示非表示 1 件の古いコメント
Junaed Hossain 2022 年 9 月 12 日
Here it is.

サインインしてコメントする。

### 採用された回答

Stephen23 2022 年 9 月 12 日

format compact
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

サインインしてコメントする。

### その他の回答 (1 件)

Cris LaPierre 2022 年 9 月 12 日

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.
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 件のコメント表示非表示 2 件の古いコメント
Torsten 2022 年 9 月 12 日

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.

サインインしてコメントする。

### カテゴリ

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

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by