You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
error with interpn function "Grid arrays must have NDGRID structure"
29 views (last 30 days)
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.
Stephen23
on 2 Feb 2022
Edited: Stephen23
on 2 Feb 2022
"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
Stephen23
on 2 Feb 2022
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.
RR
on 2 Feb 2022
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,:).
Stephen23
on 2 Feb 2022
Edited: Stephen23
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))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/881835/image.png)
plot(b(:,1),b(:,2))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/881840/image.png)
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.
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
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.
Answers (0)
See Also
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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)