Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Interpolation using interp2 function

23 views
Skip to first unread message

Katy Pointer

unread,
Oct 24, 2011, 5:00:26 AM10/24/11
to
Good morning,

I am having trouble using the interp2 function in Matlab.
I have some irregularly spaced data at coordinates in the y-z plane and want to use interp2 to interpolate the data onto a regularly spaced grid. However, when I run interp2 I get the following error;

"X and Y must be matrices produced by MESHGRID. Use TriScatteredInterp instead
of INTERP2 for scattered data."

I don't understand why I can't use my irregularly spaced coordinates instead of ones which meshgrid would produce?
Also, looking at the help for TriScatteredInterp it seems to only accept data with 1,2 or 3 columns. How would I use this function for my 27x27 data? Is there any other way to do interpolation in Matlab?

Thanks in advance

pietro

unread,
Oct 24, 2011, 7:28:13 AM10/24/11
to
What are the sizes of your X and Y data?

"Katy Pointer" wrote in message <j839f9$b59$1...@newscl01ah.mathworks.com>...

Katy Pointer

unread,
Oct 24, 2011, 7:33:14 AM10/24/11
to
The sizes are 27x27.
But I think I have found a suitable alternative to interp2; i have been using griddata, which seems to do the job.

"pietro " <braca...@email.it> wrote in message <j83i4d$5oc$1...@newscl01ah.mathworks.com>...

Steven_Lord

unread,
Oct 24, 2011, 2:48:35 PM10/24/11
to


"Katy Pointer" <kelli...@hotmail.co.uk> wrote in message
news:j839f9$b59$1...@newscl01ah.mathworks.com...
> Good morning,
>
> I am having trouble using the interp2 function in Matlab. I have some
> irregularly spaced data at coordinates in the y-z plane and want to use
> interp2 to interpolate the data onto a regularly spaced grid. However,
> when I run interp2 I get the following error;
>
> "X and Y must be matrices produced by MESHGRID. Use TriScatteredInterp
> instead
> of INTERP2 for scattered data."
>
> I don't understand why I can't use my irregularly spaced coordinates
> instead of ones which meshgrid would produce?

Because INTERP2 is intended for interpolation of gridded data rather than
scattered data. For scattered data, use TriScatteredInterp as the message
indicated.

> Also, looking at the help for TriScatteredInterp it seems to only accept
> data with 1,2 or 3 columns. How would I use this function for my 27x27
> data? Is there any other way to do interpolation in Matlab?

So you have three 27-by-27 matrices: one with x coordinates, one with y
coordinates, and one with z coordinates? Turn them into column vectors and
pass them into TriScatteredInterp.

t = TriScatteredInterp(x(:), y(:), z(:));
t(0.5, 0.5) % Interpolate at x = 0.5, y = 0.5

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Katy Pointer

unread,
Oct 25, 2011, 12:40:14 PM10/25/11
to
"Steven_Lord" <sl...@mathworks.com> wrote in message <j84bu4$89d$1...@newscl01ah.mathworks.com>...
My data lies in a plane in y-z. So I have a 27x27 matrix of y coordinates, a 27x27 matrix of z coordinates and a 27x27 matrix of measurements which correspond to each pair of yz coordinates. My problem with using TriScatteredInterp is that I can't easily turn the y and z coordinate matricies into column vectors/I don't know how to do this as all the points are at 'random' places.
For example, this is the y matrix of points;
Y =

Columns 1 through 9

-0.6342 -0.5861 -0.5379 -0.4895 -0.4410 -0.3923 -0.3436 -0.2947 -0.2457
-0.6300 -0.5823 -0.5344 -0.4863 -0.4381 -0.3898 -0.3413 -0.2928 -0.2441
-0.6257 -0.5783 -0.5307 -0.4830 -0.4351 -0.3871 -0.3390 -0.2908 -0.2424
-0.6212 -0.5742 -0.5269 -0.4795 -0.4320 -0.3843 -0.3365 -0.2887 -0.2407
-0.6165 -0.5698 -0.5229 -0.4759 -0.4287 -0.3814 -0.3340 -0.2865 -0.2389
-0.6116 -0.5653 -0.5188 -0.4721 -0.4253 -0.3784 -0.3314 -0.2842 -0.2370
-0.6066 -0.5606 -0.5145 -0.4682 -0.4218 -0.3753 -0.3286 -0.2818 -0.2350
-0.6013 -0.5558 -0.5100 -0.4642 -0.4182 -0.3720 -0.3258 -0.2794 -0.2330
-0.5959 -0.5507 -0.5054 -0.4600 -0.4144 -0.3686 -0.3228 -0.2769 -0.2309
-0.5902 -0.5455 -0.5007 -0.4556 -0.4105 -0.3652 -0.3198 -0.2743 -0.2287
-0.5844 -0.5402 -0.4957 -0.4512 -0.4064 -0.3616 -0.3166 -0.2716 -0.2264
-0.5785 -0.5346 -0.4907 -0.4465 -0.4023 -0.3579 -0.3134 -0.2688 -0.2241
-0.5723 -0.5290 -0.4854 -0.4418 -0.3980 -0.3541 -0.3101 -0.2659 -0.2217
-0.5660 -0.5231 -0.4801 -0.4369 -0.3936 -0.3502 -0.3066 -0.2630 -0.2193
-0.5595 -0.5171 -0.4746 -0.4319 -0.3891 -0.3461 -0.3031 -0.2600 -0.2168
-0.5528 -0.5109 -0.4689 -0.4267 -0.3844 -0.3420 -0.2995 -0.2569 -0.2142
-0.5460 -0.5046 -0.4631 -0.4215 -0.3797 -0.3378 -0.2958 -0.2537 -0.2115
-0.5390 -0.4981 -0.4572 -0.4160 -0.3748 -0.3334 -0.2920 -0.2504 -0.2088
-0.5318 -0.4915 -0.4511 -0.4105 -0.3698 -0.3290 -0.2881 -0.2471 -0.2060
-0.5245 -0.4847 -0.4449 -0.4048 -0.3647 -0.3245 -0.2841 -0.2437 -0.2032
-0.5170 -0.4778 -0.4385 -0.3991 -0.3595 -0.3198 -0.2801 -0.2402 -0.2003
-0.5093 -0.4707 -0.4320 -0.3932 -0.3542 -0.3151 -0.2759 -0.2367 -0.1973
-0.5015 -0.4635 -0.4254 -0.3871 -0.3488 -0.3103 -0.2717 -0.2330 -0.1943
-0.4936 -0.4562 -0.4186 -0.3810 -0.3432 -0.3054 -0.2674 -0.2293 -0.1912
-0.4854 -0.4487 -0.4118 -0.3747 -0.3376 -0.3003 -0.2630 -0.2256 -0.1881
-0.4772 -0.4410 -0.4048 -0.3684 -0.3318 -0.2952 -0.2585 -0.2217 -0.1849
-0.4688 -0.4333 -0.3976 -0.3619 -0.3260 -0.2900 -0.2540 -0.2178 -0.1816

Columns 10 through 18

-0.1966 -0.1475 -0.0984 -0.0492 0 0.0492 0.0984 0.1475 0.1966
-0.1954 -0.1466 -0.0977 -0.0489 0 0.0489 0.0977 0.1466 0.1954
-0.1940 -0.1456 -0.0971 -0.0485 0 0.0485 0.0971 0.1456 0.1940
-0.1926 -0.1445 -0.0964 -0.0482 0 0.0482 0.0964 0.1445 0.1926
-0.1912 -0.1434 -0.0956 -0.0478 0 0.0478 0.0956 0.1434 0.1912
-0.1897 -0.1423 -0.0949 -0.0475 0 0.0475 0.0949 0.1423 0.1897
-0.1881 -0.1411 -0.0941 -0.0471 0 0.0471 0.0941 0.1411 0.1881
-0.1865 -0.1399 -0.0933 -0.0467 0 0.0467 0.0933 0.1399 0.1865
-0.1848 -0.1386 -0.0924 -0.0462 0 0.0462 0.0924 0.1386 0.1848
-0.1830 -0.1373 -0.0916 -0.0458 0 0.0458 0.0916 0.1373 0.1830
-0.1812 -0.1360 -0.0907 -0.0453 0 0.0453 0.0907 0.1360 0.1812
-0.1794 -0.1346 -0.0897 -0.0449 0 0.0449 0.0897 0.1346 0.1794
-0.1775 -0.1332 -0.0888 -0.0444 0 0.0444 0.0888 0.1332 0.1775
-0.1755 -0.1317 -0.0878 -0.0439 0 0.0439 0.0878 0.1317 0.1755
-0.1735 -0.1302 -0.0868 -0.0434 0 0.0434 0.0868 0.1302 0.1735
-0.1714 -0.1286 -0.0858 -0.0429 0 0.0429 0.0858 0.1286 0.1714
-0.1693 -0.1270 -0.0847 -0.0424 0 0.0424 0.0847 0.1270 0.1693
-0.1671 -0.1254 -0.0836 -0.0418 0 0.0418 0.0836 0.1254 0.1671
-0.1649 -0.1237 -0.0825 -0.0413 0 0.0413 0.0825 0.1237 0.1649
-0.1626 -0.1220 -0.0814 -0.0407 0 0.0407 0.0814 0.1220 0.1626
-0.1603 -0.1203 -0.0802 -0.0401 0 0.0401 0.0802 0.1203 0.1603
-0.1579 -0.1185 -0.0790 -0.0395 0 0.0395 0.0790 0.1185 0.1579
-0.1555 -0.1167 -0.0778 -0.0389 0 0.0389 0.0778 0.1167 0.1555
-0.1530 -0.1148 -0.0766 -0.0383 0 0.0383 0.0766 0.1148 0.1530
-0.1505 -0.1129 -0.0753 -0.0377 0 0.0377 0.0753 0.1129 0.1505
-0.1480 -0.1110 -0.0740 -0.0370 0 0.0370 0.0740 0.1110 0.1480
-0.1454 -0.1091 -0.0727 -0.0364 0 0.0364 0.0727 0.1091 0.1454

Columns 19 through 27

0.2457 0.2947 0.3436 0.3923 0.4410 0.4895 0.5379 0.5861 0.6342
0.2441 0.2928 0.3413 0.3898 0.4381 0.4863 0.5344 0.5823 0.6300
0.2424 0.2908 0.3390 0.3871 0.4351 0.4830 0.5307 0.5783 0.6257
0.2407 0.2887 0.3365 0.3843 0.4320 0.4795 0.5269 0.5742 0.6212
0.2389 0.2865 0.3340 0.3814 0.4287 0.4759 0.5229 0.5698 0.6165
0.2370 0.2842 0.3314 0.3784 0.4253 0.4721 0.5188 0.5653 0.6116
0.2350 0.2818 0.3286 0.3753 0.4218 0.4682 0.5145 0.5606 0.6066
0.2330 0.2794 0.3258 0.3720 0.4182 0.4642 0.5100 0.5558 0.6013
0.2309 0.2769 0.3228 0.3686 0.4144 0.4600 0.5054 0.5507 0.5959
0.2287 0.2743 0.3198 0.3652 0.4105 0.4556 0.5007 0.5455 0.5902
0.2264 0.2716 0.3166 0.3616 0.4064 0.4512 0.4957 0.5402 0.5844
0.2241 0.2688 0.3134 0.3579 0.4023 0.4465 0.4907 0.5346 0.5785
0.2217 0.2659 0.3101 0.3541 0.3980 0.4418 0.4854 0.5290 0.5723
0.2193 0.2630 0.3066 0.3502 0.3936 0.4369 0.4801 0.5231 0.5660
0.2168 0.2600 0.3031 0.3461 0.3891 0.4319 0.4746 0.5171 0.5595
0.2142 0.2569 0.2995 0.3420 0.3844 0.4267 0.4689 0.5109 0.5528
0.2115 0.2537 0.2958 0.3378 0.3797 0.4215 0.4631 0.5046 0.5460
0.2088 0.2504 0.2920 0.3334 0.3748 0.4160 0.4572 0.4981 0.5390
0.2060 0.2471 0.2881 0.3290 0.3698 0.4105 0.4511 0.4915 0.5318
0.2032 0.2437 0.2841 0.3245 0.3647 0.4048 0.4449 0.4847 0.5245
0.2003 0.2402 0.2801 0.3198 0.3595 0.3991 0.4385 0.4778 0.5170
0.1973 0.2367 0.2759 0.3151 0.3542 0.3932 0.4320 0.4707 0.5093
0.1943 0.2330 0.2717 0.3103 0.3488 0.3871 0.4254 0.4635 0.5015
0.1912 0.2293 0.2674 0.3054 0.3432 0.3810 0.4186 0.4562 0.4936
0.1881 0.2256 0.2630 0.3003 0.3376 0.3747 0.4118 0.4487 0.4854
0.1849 0.2217 0.2585 0.2952 0.3318 0.3684 0.4048 0.4410 0.4772
0.1816 0.2178 0.2540 0.2900 0.3260 0.3619 0.3976 0.4333 0.4688
Regards

Matt J

unread,
Oct 25, 2011, 12:59:29 PM10/25/11
to
"Katy Pointer" wrote in message <j86ope$8st$1...@newscl01ah.mathworks.com>...
>
>
> My data lies in a plane in y-z. So I have a 27x27 matrix of y coordinates, a 27x27 matrix of z coordinates and a 27x27 matrix of measurements which correspond to each pair of yz coordinates. My problem with using TriScatteredInterp is that I can't easily turn the y and z coordinate matricies into column vectors/I don't know how to do this as all the points are at 'random' places.
> For example, this is the y matrix of points;
==================

To turn a matrix into a column vector use the colon operator,e.g.

>> Y=[1 2 3; 4 5 6]

Y =

1 2 3
4 5 6

>> Y(:)

ans =

1
4
2
5
3
6


You could also use the RESHAPE command

Steven_Lord

unread,
Oct 25, 2011, 1:01:05 PM10/25/11
to


"Katy Pointer" <kelli...@hotmail.co.uk> wrote in message
news:j86ope$8st$1...@newscl01ah.mathworks.com...
See my TriScatteredInterp call above, but rename the variables.

t = TriScatteredInterp(y(:), z(:), measurements(:));
t(0.5, 0.75) % Determine the interpolated value at y = 0.5, z = 0.75

Katy Pointer

unread,
Oct 26, 2011, 5:02:14 AM10/26/11
to
"Matt J" wrote in message <j86pth$com$1...@newscl01ah.mathworks.com>...
Ahhhh, I see. Thanks very much!
0 new messages