error with interpn function "Grid arrays must have NDGRID structure"

Dear all,
I have two matrix a=10x2 and b= 10x2. they are the indipendent variables of an unknown function C(a,b)= 10x3. I would like to evaluate this function in Aq (100x2) and Bq (100x2) variable.
a % 10x2 matrix
b % 10x2 matrix
C % 10x3 matrix
% difine Aq 100x2 and Bq 100x2
a1=[min(a(:,1)):1:max(a(:,1))]';
a2=[min(a(:,2)):1:max(a(:,2))]';
Aq(:,1)=a1(1:100);
Aq(:,2)=a2(1:100);
b1=[min(b(:,1)):1:max(b(:,1))]';
b2=[min(b(:,2)):1:max(b(:,2))]';
Bq(:,1)=b1(1:100);
Bq(:,2)=b2(1:100);
%interp function C at Aq and Bq;
Cq = interpn(a,b,C,Aq,Bq)
I have this error
"Error using griddedInterpolant
Grid arrays must have NDGRID structure.
Error in interpn (line 151)
F = griddedInterpolant(X{:}, V, method,extrap);"
Why? what is wrong in my code?
thank you very much in advance
RR

13 Comments

"I have two matrix a=10x2 and b= 10x2..."
You have not told us the most important information, which is how the data are arranged within those matrices.
Please upload the matrices a, b, and C in one .mat file by clicking the paperclip button.
To use interpn a,b,c should be 3D matrices...also note that a, b,c should be of same dimensions. In your case it is 10x3. Also note that, in this case interp2 will work. You need not to use interpn.
I suggest you consider
Aq = [linspace(min(a(:,1)), max(a(:,1)), 100).',
linspace(min(a(:,2)), max(a(:,2)), 100).'];
and similar for Bq .
This solves the problem that the data might not have a span of more than 100, and allows the entire range of values to be queried.
This will not solve the problem you posted about.
"To use interpn a,b,c should be 3D matrices"
Why?
The NDGRID documentation states on its very first line: "Interpolation for 1-D, 2-D, 3-D, and N-D gridded data in ndgrid format". Do you have any specific reason why you think the MATLAB documentation is incorrect?
For further information I have attached the a, b, C matrices.
Your data are nothing like an NDGRID or a MESHGRID format:
S = load('a.mat');
a = S.a
a = 10×2
592.3170 802.7369 641.4783 816.1367 601.7446 833.2044 551.7703 820.0732 502.1027 805.9781 543.2049 788.8682 584.3629 772.0326 633.0912 785.0601 592.3600 741.6462 592.2411 861.2977
S = load('b.mat');
b = S.b
b = 10×2
571.4879 792.3392 616.3365 777.2827 661.8543 791.9391 617.7148 807.6232 572.5029 823.4482 526.8819 807.6869 480.6655 792.4200 525.3388 776.6940 571.4518 732.7923 571.6856 851.9971
S = load('C.mat');
C = S.C
C = 10×3
0 0 0 0 10 0 10 10 0 10 0 0 10 -10 0 0 -10 0 -10 -10 0 -10 0 0 0 0 10 0 0 -10
How is the data structured within those arrays?
Tip for the future: saving all of the variables into one .mat file as requested is simpler for everyone.
I am sorry for the inconvenience for the splitted variables.
Each row of the matrices corresponds to an observation: for example a(1,:) and b(1,:) generates C(1,:).
@RR: Do a and b represent scattered sample points in a four-dimensional space? This is how they look plotted:
S = load('a.mat');
a = S.a;
S = load('b.mat');
b = S.b;
S = load('C.mat');
C = S.C;
plot3(a(:,1),a(:,2))
plot(b(:,1),b(:,2))
If the data are 10 scattered points in some space, how do you want to subdivide that space into 100 points? Do you wish to subdivide the implied line connecting those points (i.e. like a parametric function) ?
Or do you wish to divide up the space in some other way (e.g. gridded, triangulated, etc.) ?
You cannot use interpn() for data with that structure. You need to use one of the scattered interpolation methods, on a row-by-row basis. scatteredInterpolant() might be most appropriate.
Dear @Stephen, thank you for your questions. I wish to divide the space as a grid for a and for b variables so tat the function C can be evaluated in correspondance of some points in a (2D dimensional) and b (2D dimensional). HOw can i do this?
If you want a to be 2 dimensional and b to be 2 dimensional, and C to work with that, then you are effectively asking that C is a function over 4 dimensions. You would have to use something like https://www.mathworks.com/help/matlab/ref/griddatan.html
It looks to me as if you do not have enough data to produce a robust 4 dimensional fit.
Unfortunately griddatan outputs vq with is a vector of length p (p= number observation) while I would like to obtain instead a matrix of size px3.
Do three separate griddatan(), one for each column of C.

Sign in to comment.

Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Asked:

RR
on 2 Feb 2022

Commented:

on 2 Feb 2022

Community Treasure Hunt

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

Start Hunting!