bug with interp2 ?

8 views (last 30 days)
Timbro
Timbro on 2 May 2023
Commented: Timbro on 3 May 2023
Hello,
I want to interpolate a 2D data "v" from a 2km latitude and longitude grid (xx and yy) to a 10 km grid (Xq and Yq). but with interp2 I get the following error :
>> [Xq,Yq] = ndgrid(lon_extraction,lat_extraction); %
>> whos xx yy v X Y
Name Size Bytes Class Attributes
X 23x83 15272 double
Y 23x83 15272 double
v 407x793 2582008 double
xx 407x793 2582008 double
yy 407x793 2582008 double
>> h_extr_interp = interp2(xx,yy, v, X,Y);
Error using griddedInterpolant
Grid arrays must have NDGRID structure.
Error in interp2>makegriddedinterp (line 226)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
>>
(I get the same result using meshgrid instead of ndgrid)
any ideas why I get this message??
  3 Comments
Stephen23
Stephen23 on 3 May 2023
Edited: Stephen23 on 3 May 2023
"(I get the same result using meshgrid instead of ndgrid)"
Your code shows that you do not use the outputs of NDGRID, so changing the function to MESHGRID will not change that.
"Problem solved using griddata instead of interp2"
If you have data in ND-grid format, then use INTERPN:
The MATLAB documentation explains how to select the operator based on the data arrangement:
Matt J
Matt J on 3 May 2023
Problem solved using griddata instead of interp2
griddata is quite a bit slower than interp2. You should not use it unless you are certain your xx,yy locations don't define a regular lattice.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 2 May 2023
Moved: Walter Roberson on 2 May 2023
[Xq,Yq] = ndgrid(lon_extraction,lat_extraction);
h_extr_interp = interp2(xx,yy, v, X,Y);
What do xx and yy or X and Y have to do with Xq and Yq ? You build an ndgrid but you do not use it in the interp2
  5 Comments
Stephen23
Stephen23 on 3 May 2023
Edited: Stephen23 on 3 May 2023
Your data are scattered, they are not on a grid:
S = load('test_bug.mat')
S = struct with fields:
Xq: [23×83 double] Yq: [23×83 double] v: [407×793 double] xx: [407×793 double] yy: [407×793 double]
S.xx
ans = 407×793
-19.9868 -19.9849 -19.9830 -19.9811 -19.9792 -19.9774 -19.9755 -19.9736 -19.9717 -19.9698 -19.9679 -19.9660 -19.9641 -19.9622 -19.9603 -19.9585 -19.9566 -19.9547 -19.9528 -19.9509 -19.9490 -19.9471 -19.9453 -19.9434 -19.9415 -19.9396 -19.9377 -19.9358 -19.9340 -19.9321 -19.9627 -19.9608 -19.9589 -19.9570 -19.9551 -19.9532 -19.9513 -19.9494 -19.9475 -19.9456 -19.9437 -19.9419 -19.9400 -19.9381 -19.9362 -19.9343 -19.9324 -19.9305 -19.9286 -19.9267 -19.9249 -19.9230 -19.9211 -19.9192 -19.9173 -19.9154 -19.9135 -19.9117 -19.9098 -19.9079 -19.9385 -19.9366 -19.9347 -19.9328 -19.9310 -19.9291 -19.9272 -19.9253 -19.9234 -19.9215 -19.9196 -19.9177 -19.9158 -19.9139 -19.9120 -19.9101 -19.9082 -19.9064 -19.9045 -19.9026 -19.9007 -19.8988 -19.8969 -19.8950 -19.8931 -19.8912 -19.8894 -19.8875 -19.8856 -19.8837 -19.9144 -19.9125 -19.9106 -19.9087 -19.9068 -19.9049 -19.9030 -19.9011 -19.8992 -19.8973 -19.8954 -19.8935 -19.8916 -19.8898 -19.8879 -19.8860 -19.8841 -19.8822 -19.8803 -19.8784 -19.8765 -19.8746 -19.8727 -19.8708 -19.8690 -19.8671 -19.8652 -19.8633 -19.8614 -19.8595 -19.8902 -19.8883 -19.8864 -19.8846 -19.8827 -19.8808 -19.8789 -19.8770 -19.8751 -19.8732 -19.8713 -19.8694 -19.8675 -19.8656 -19.8637 -19.8618 -19.8599 -19.8580 -19.8561 -19.8542 -19.8523 -19.8505 -19.8486 -19.8467 -19.8448 -19.8429 -19.8410 -19.8391 -19.8372 -19.8353 -19.8661 -19.8642 -19.8623 -19.8604 -19.8585 -19.8566 -19.8547 -19.8528 -19.8509 -19.8490 -19.8471 -19.8452 -19.8433 -19.8414 -19.8395 -19.8376 -19.8358 -19.8339 -19.8320 -19.8301 -19.8282 -19.8263 -19.8244 -19.8225 -19.8206 -19.8187 -19.8168 -19.8149 -19.8130 -19.8112 -19.8420 -19.8401 -19.8382 -19.8363 -19.8344 -19.8325 -19.8306 -19.8287 -19.8268 -19.8249 -19.8230 -19.8211 -19.8192 -19.8173 -19.8154 -19.8135 -19.8116 -19.8097 -19.8078 -19.8059 -19.8040 -19.8021 -19.8002 -19.7983 -19.7964 -19.7945 -19.7926 -19.7908 -19.7889 -19.7870 -19.8178 -19.8159 -19.8140 -19.8121 -19.8102 -19.8083 -19.8064 -19.8045 -19.8026 -19.8007 -19.7988 -19.7969 -19.7950 -19.7931 -19.7912 -19.7893 -19.7874 -19.7855 -19.7836 -19.7817 -19.7798 -19.7779 -19.7760 -19.7741 -19.7723 -19.7704 -19.7685 -19.7666 -19.7647 -19.7628 -19.7937 -19.7918 -19.7899 -19.7880 -19.7861 -19.7842 -19.7823 -19.7804 -19.7785 -19.7766 -19.7747 -19.7728 -19.7709 -19.7690 -19.7671 -19.7652 -19.7633 -19.7614 -19.7595 -19.7576 -19.7557 -19.7538 -19.7519 -19.7500 -19.7481 -19.7462 -19.7443 -19.7424 -19.7405 -19.7386 -19.7695 -19.7676 -19.7657 -19.7638 -19.7619 -19.7600 -19.7581 -19.7562 -19.7543 -19.7524 -19.7505 -19.7486 -19.7467 -19.7448 -19.7429 -19.7410 -19.7391 -19.7372 -19.7353 -19.7334 -19.7315 -19.7296 -19.7277 -19.7258 -19.7239 -19.7220 -19.7201 -19.7182 -19.7163 -19.7144
S.yy
ans = 407×793
7.0762 7.1011 7.1261 7.1510 7.1759 7.2009 7.2258 7.2507 7.2756 7.3006 7.3255 7.3504 7.3754 7.4003 7.4252 7.4501 7.4751 7.5000 7.5249 7.5499 7.5748 7.5997 7.6246 7.6496 7.6745 7.6994 7.7244 7.7493 7.7742 7.7991 7.0744 7.0993 7.1243 7.1492 7.1741 7.1991 7.2240 7.2489 7.2738 7.2988 7.3237 7.3486 7.3736 7.3985 7.4234 7.4483 7.4733 7.4982 7.5231 7.5481 7.5730 7.5979 7.6228 7.6478 7.6727 7.6976 7.7226 7.7475 7.7724 7.7973 7.0726 7.0975 7.1225 7.1474 7.1723 7.1973 7.2222 7.2471 7.2720 7.2970 7.3219 7.3468 7.3718 7.3967 7.4216 7.4465 7.4715 7.4964 7.5213 7.5463 7.5712 7.5961 7.6210 7.6460 7.6709 7.6958 7.7208 7.7457 7.7706 7.7956 7.0708 7.0957 7.1207 7.1456 7.1705 7.1954 7.2204 7.2453 7.2702 7.2952 7.3201 7.3450 7.3700 7.3949 7.4198 7.4447 7.4697 7.4946 7.5195 7.5445 7.5694 7.5943 7.6192 7.6442 7.6691 7.6940 7.7190 7.7439 7.7688 7.7938 7.0690 7.0939 7.1188 7.1438 7.1687 7.1936 7.2186 7.2435 7.2684 7.2934 7.3183 7.3432 7.3681 7.3931 7.4180 7.4429 7.4679 7.4928 7.5177 7.5427 7.5676 7.5925 7.6174 7.6424 7.6673 7.6922 7.7172 7.7421 7.7670 7.7920 7.0672 7.0921 7.1170 7.1420 7.1669 7.1918 7.2168 7.2417 7.2666 7.2915 7.3165 7.3414 7.3663 7.3913 7.4162 7.4411 7.4661 7.4910 7.5159 7.5409 7.5658 7.5907 7.6156 7.6406 7.6655 7.6904 7.7154 7.7403 7.7652 7.7902 7.0654 7.0903 7.1152 7.1402 7.1651 7.1900 7.2149 7.2399 7.2648 7.2897 7.3147 7.3396 7.3645 7.3895 7.4144 7.4393 7.4643 7.4892 7.5141 7.5390 7.5640 7.5889 7.6138 7.6388 7.6637 7.6886 7.7136 7.7385 7.7634 7.7884 7.0636 7.0885 7.1134 7.1383 7.1633 7.1882 7.2131 7.2381 7.2630 7.2879 7.3129 7.3378 7.3627 7.3877 7.4126 7.4375 7.4624 7.4874 7.5123 7.5372 7.5622 7.5871 7.6120 7.6370 7.6619 7.6868 7.7118 7.7367 7.7616 7.7866 7.0617 7.0867 7.1116 7.1365 7.1615 7.1864 7.2113 7.2363 7.2612 7.2861 7.3111 7.3360 7.3609 7.3858 7.4108 7.4357 7.4606 7.4856 7.5105 7.5354 7.5604 7.5853 7.6102 7.6352 7.6601 7.6850 7.7100 7.7349 7.7598 7.7848 7.0599 7.0849 7.1098 7.1347 7.1596 7.1846 7.2095 7.2344 7.2594 7.2843 7.3092 7.3342 7.3591 7.3840 7.4090 7.4339 7.4588 7.4838 7.5087 7.5336 7.5586 7.5835 7.6084 7.6334 7.6583 7.6832 7.7082 7.7331 7.7580 7.7829
The rows and columns clearly do not consist of identical values. Therefore your data are not gridded.
Compare against the perfectly gridded sample points returned by NDGRID or MESHGRID:
[X,Y] = meshgrid(0:3,4:7)
X = 4×4
0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3
Y = 4×4
4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7
So you will need to use a scattered interpolant (not a gridded interpolant) e.g. GRIDDATA:
Timbro
Timbro on 3 May 2023
Thank you Stephen, I understand now

Sign in to comment.

Categories

Find more on Interpolation in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!