error with interpn function "Grid arrays must have NDGRID structure"
Show older comments
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
Stephen23
on 2 Feb 2022
"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.
KSSV
on 2 Feb 2022
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.
Walter Roberson
on 2 Feb 2022
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?
RR
on 2 Feb 2022
Your data are nothing like an NDGRID or a MESHGRID format:
S = load('a.mat');
a = S.a
S = load('b.mat');
b = S.b
S = load('C.mat');
C = S.C
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.
RR
on 2 Feb 2022
@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.) ?
Walter Roberson
on 2 Feb 2022
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.
RR
on 2 Feb 2022
Walter Roberson
on 2 Feb 2022
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.
RR
on 2 Feb 2022
Walter Roberson
on 2 Feb 2022
Do three separate griddatan(), one for each column of C.
Answers (0)
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!
