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

Timbro
Timbro 2023년 5월 2일
Problem solved using griddata instead of interp2
Stephen23
Stephen23 2023년 5월 3일
편집: Stephen23 2023년 5월 3일
"(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 2023년 5월 3일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 5월 2일
이동: Walter Roberson 2023년 5월 2일

1 개 추천

[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

Timbro
Timbro 2023년 5월 3일
Thank you for your comments, indead, sorry I mixed in my code, but actually here X = Xq and Yq = Y
Anyway, using griddata instead of interp2 solved the problem, although I still don't undestand the error I get with interp2. Maybe interp2 can only be used to switch from a grid to a finer grid, but not the opposite.
Best regards
Stephen23
Stephen23 2023년 5월 3일
편집: Stephen23 2023년 5월 3일
"Maybe interp2 can only be used to switch from a grid to a finer grid, but not the opposite."
No.
Lets try it right now, modifying the first example from the documentation:
[X,Y] = meshgrid(-3:3,-2:2);
V = peaks(X,Y)
V = 5×7
0.0007 0.0468 -0.5921 -4.7596 -2.1024 -0.0616 0.0004 -0.0088 -0.1301 1.8559 -0.7239 -0.2729 0.4996 0.0130 -0.0365 -1.3327 -1.6523 0.9810 2.9369 1.4122 0.0331 -0.0137 -0.4808 0.2289 3.6886 2.4338 0.5805 0.0125 0.0000 0.0797 2.0967 5.8591 2.2099 0.1328 0.0013
Finer grid:
[Xq,Yq] = meshgrid(-3:0.25:3,-2:0.25:2);
Vq = interp2(X,Y,V,Xq,Yq)
Vq = 17×25
0.0007 0.0123 0.0238 0.0353 0.0468 -0.1129 -0.2726 -0.4324 -0.5921 -1.6340 -2.6759 -3.7177 -4.7596 -4.0953 -3.4310 -2.7667 -2.1024 -1.5922 -1.0820 -0.5718 -0.0616 -0.0461 -0.0306 -0.0151 0.0004 -0.0016 -0.0006 0.0005 0.0016 0.0026 0.0069 0.0112 0.0156 0.0199 -0.9228 -1.8654 -2.8080 -3.7507 -3.2243 -2.6978 -2.1714 -1.6450 -1.2141 -0.7832 -0.3522 0.0787 0.0599 0.0411 0.0223 0.0036 -0.0040 -0.0134 -0.0228 -0.0322 -0.0416 0.1268 0.2951 0.4635 0.6319 -0.2115 -1.0549 -1.8983 -2.7418 -2.3532 -1.9647 -1.5762 -1.1876 -0.8360 -0.4843 -0.1327 0.2190 0.1659 0.1129 0.0598 0.0067 -0.0064 -0.0262 -0.0461 -0.0660 -0.0858 0.2466 0.5790 0.9115 1.2439 0.4997 -0.2445 -0.9887 -1.7328 -1.4822 -1.2316 -0.9809 -0.7303 -0.4579 -0.1855 0.0869 0.3593 0.2720 0.1846 0.0972 0.0099 -0.0088 -0.0391 -0.0694 -0.0997 -0.1301 0.3664 0.8629 1.3594 1.8559 1.2109 0.5660 -0.0790 -0.7239 -0.6112 -0.4984 -0.3857 -0.2729 -0.0798 0.1134 0.3065 0.4996 0.3780 0.2563 0.1347 0.0130 -0.0157 -0.1195 -0.2232 -0.3270 -0.4307 -0.0783 0.2741 0.6264 0.9788 0.6597 0.3406 0.0215 -0.2977 -0.0909 0.1159 0.3227 0.5295 0.5791 0.6287 0.6782 0.7278 0.5503 0.3729 0.1955 0.0180 -0.0226 -0.1998 -0.3770 -0.5542 -0.7314 -0.5231 -0.3148 -0.1065 0.1018 0.1085 0.1152 0.1219 0.1286 0.4294 0.7303 1.0311 1.3320 1.2380 1.1440 1.0499 0.9559 0.7227 0.4895 0.2563 0.0231 -0.0296 -0.2802 -0.5308 -0.7814 -1.0320 -0.9678 -0.9037 -0.8395 -0.7753 -0.4428 -0.1103 0.2223 0.5548 0.9497 1.3446 1.7395 2.1345 1.8969 1.6592 1.4216 1.1840 0.8950 0.6061 0.3171 0.0281 -0.0365 -0.3606 -0.6846 -1.0086 -1.3327 -1.4126 -1.4925 -1.5724 -1.6523 -0.9940 -0.3357 0.3227 0.9810 1.4700 1.9590 2.4480 2.9369 2.5557 2.1745 1.7934 1.4122 1.0674 0.7226 0.3779 0.0331 -0.0308 -0.3030 -0.5753 -0.8475 -1.1197 -1.1353 -1.1509 -1.1665 -1.1820 -0.4720 0.2379 0.9479 1.6579 1.9462 2.2345 2.5228 2.8111 2.4094 2.0077 1.6060 1.2042 0.9102 0.6161 0.3220 0.0280
Courser grid:
[Xq,Yq] = meshgrid(-3:2:3,-2:2:2);
Vq = interp2(X,Y,V,Xq,Yq)
Vq = 3×4
0.0007 -0.5921 -2.1024 0.0004 -0.0365 -1.6523 2.9369 0.0331 0.0000 2.0967 2.2099 0.0013
No errors, no problems, everything works exactly as documented.
"bug with interp2 ?"
I very much doubt that.
Most likely the problem is the format of your data, which so far you have not provided us with. For example, if the data are actually in ND-grid format:
[X,Y] = ndgrid(-3:3,-2:2);
V = peaks(X,Y) % not meshgrid data
V = 7×5
0.0007 -0.0088 -0.0365 -0.0137 0.0000 0.0468 -0.1301 -1.3327 -0.4808 0.0797 -0.5921 1.8559 -1.6523 0.2289 2.0967 -4.7596 -0.7239 0.9810 3.6886 5.8591 -2.1024 -0.2729 2.9369 2.4338 2.2099 -0.0616 0.4996 1.4122 0.5805 0.1328 0.0004 0.0130 0.0331 0.0125 0.0013
[Xq,Yq] = meshgrid(-3:2:3,-2:2:2); % what you use here is irrelevant
Vq = interp2(X,Y,V,Xq,Yq) % ... because X & Y still have the wrong format
Error using interp2>makegriddedinterp
Input grid is not a valid MESHGRID.

Error in interp2 (line 134)
F = makegriddedinterp(X, Y, V, method,extrap);
If you either uploaded or showed your actual data, then we could check its format. Until you do that, we can safely assume that its format is incorrect for the interpolation function that you have chosen.
Timbro
Timbro 2023년 5월 3일
이동: Stephen23 2023년 5월 3일
Indeed it must be a problem in the format of data; I attached the data to this message
Stephen23
Stephen23 2023년 5월 3일
편집: Stephen23 2023년 5월 3일
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 2023년 5월 3일
Thank you Stephen, I understand now

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

제품

릴리스

R2022a

질문:

2023년 5월 2일

댓글:

2023년 5월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by