I have a data set in four variables (alpha,beta,gamma,lambda).
Here alpha is a 10 element vector, beta is a 8 element vector, both gamma and lambda have four values each.
The data is given such that for each value of lambda, there are 4 tables, and those 4 tables correspond to each value of gamma. Each of those four tables is a 8x10 matrix whose rows correspond to values of beta and columns correspond to values of alpha.
I tried to implement it this way (sample code using random values):
alpha = 1:10;
beta = 1:8;
gamma = 1:4;
lambda = 1:4;
m = length(beta);
n = length(alpha);
Data(:,:,1,1) = rand(m,n); % alpha in first dimension, beta is 2nd dimension, gamma in 3rd and lambda in 4th
Data(:,:,2,1) = rand(m,n);
Data(:,:,3,1) = rand(m,n);
Data(:,:,4,1) = rand(m,n);
Data(:,:,1,2) = rand(m,n);
Data(:,:,2,2) = rand(m,n);
Data(:,:,3,2) = rand(m,n);
Data(:,:,4,2) = rand(m,n);
Data(:,:,1,3) = rand(m,n);
Data(:,:,2,3) = rand(m,n);
Data(:,:,3,3) = rand(m,n);
Data(:,:,4,3) = rand(m,n);
Data(:,:,1,4) = rand(m,n);
Data(:,:,2,4) = rand(m,n);
Data(:,:,3,4) = rand(m,n);
Data(:,:,4,4) = rand(m,n);
idat = interpn(alpha,beta,gamma,lambda,Data,1,2,3,4);
Error using griddedInterpolant
Sample points vector corresponding to grid dimension 1 must contain 8 elements.

Error in interpn (line 149)
F = griddedInterpolant(X, V, method,extrap);
This code gives me errors. How can I resolve it? Do I need to create an n-dimensional grid? or am I making some mistake in the making of 4D tables.

 採用された回答

Voss
Voss 2023 年 2 月 5 日
編集済み: Voss 2023 年 2 月 5 日

0 投票

Swap your definitions of m and n (or equivalently use rand(n,m)). Like you said, alpha is 1st dimension, beta is 2nd.
alpha = 1:10;
beta = 1:8;
gamma = 1:4;
lambda = 1:4;
m = length(alpha);
n = length(beta);
Data(:,:,1,1) = rand(m,n); % alpha in first dimension, beta is 2nd dimension, gamma in 3rd and lambda in 4th
Data(:,:,2,1) = rand(m,n);
Data(:,:,3,1) = rand(m,n);
Data(:,:,4,1) = rand(m,n);
Data(:,:,1,2) = rand(m,n);
Data(:,:,2,2) = rand(m,n);
Data(:,:,3,2) = rand(m,n);
Data(:,:,4,2) = rand(m,n);
Data(:,:,1,3) = rand(m,n);
Data(:,:,2,3) = rand(m,n);
Data(:,:,3,3) = rand(m,n);
Data(:,:,4,3) = rand(m,n);
Data(:,:,1,4) = rand(m,n);
Data(:,:,2,4) = rand(m,n);
Data(:,:,3,4) = rand(m,n);
Data(:,:,4,4) = rand(m,n);
idat = interpn(alpha,beta,gamma,lambda,Data,1,2,3,4);
disp(idat)
0.7127
Edit to show that the interpolated value matches the input table:
Data(1,2,3,4)
ans = 0.7127

7 件のコメント

Fawad Khan
Fawad Khan 2023 年 2 月 5 日
You're right. But, I actually followed the 2D interpolation first. Like I said, that the matrices have size 8 by 10, where the rows correspond to beta and columns correspond to alpha. If I ignore 3rd and 4th dimensions for now. Then, the following code works
alpha = 1:10;
beta = 1:8;
m = length(beta);
n = length(alpha);
Data = rand(m,n);
idat = interp2(alpha,beta,Data,1,2)
idat = 0.7148
However, if I were to swap the dimensions in this case:
alpha = 1:10;
beta = 1:8;
m = length(alpha);
n = length(beta);
Data = rand(m,n);
idat = interp2(alpha,beta,Data,1,2)
Error using griddedInterpolant
Sample points vector corresponding to grid dimension 1 must contain 8 elements.

Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});

Error in interp2 (line 126)
F = makegriddedinterp({X, Y}, V, method,extrap);
then I get the same error.
Fawad Khan
Fawad Khan 2023 年 2 月 5 日
編集済み: Fawad Khan 2023 年 2 月 5 日
Also, if you print the corresponding data table value from your solution
Data(1,2,3,4)
The interpolated value corresponds to a value for beta = 1 and alpha = 2 whereas I wanted the value corresponding to alpha = 1 and beta = 2
Fawad Khan
Fawad Khan 2023 年 2 月 5 日
You can also check the documentation of interp2. It says "The default grid points cover the rectangular region, X=1:n and Y=1:m, where [m,n] = size(V)."
Voss
Voss 2023 年 2 月 5 日
From the interp2 documentation:
"If X and Y are grid vectors, then V must be a matrix containing length(Y) rows and length(X) columns."
From the interpn documentation:
"If X1,X2,...,Xn are grid vectors, then V is an array whose ith dimension is the same length as grid vector Xi, where i= 1,2,...n."
So interp2 is reversed relative to what you'd expect going by how interpn works.
Voss
Voss 2023 年 2 月 5 日
Also, Data(1,2,3,4) matches the result from interpn. I've edited my answer to show that.
Fawad Khan
Fawad Khan 2023 年 2 月 5 日
Yes, you're actually right. Thank you for your help.
Voss
Voss 2023 年 2 月 5 日
You're welcome.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

製品

リリース

R2021a

質問済み:

2023 年 2 月 5 日

コメント済み:

2023 年 2 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by