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

interpolating between polar and cartesian grids

1,108 views
Skip to first unread message

Jeremy

unread,
Nov 8, 2011, 6:48:28 PM11/8/11
to
Hello,

I am having a very strange issue with interpolation that I cannot seem to debug - I am hoping fresh eyes will help. The problem is best described if I explain it as follows:

I have cartesian data Z(x,y) where x and y are vectors that describe the locations of the values of Z. To get this data onto a polar grid described by r and th (theta), I just do

[thg,rg] = meshgrid(thetac,rc);
[xn,yn] = pol2cart(thg,rg);
Z_rth = interp2(x,y,Z,xn,yn);

which works fine. I have linearly interpolated from the original cartesian grid to the cartesian version of my polar grid. Great. Now, for some reason I can't seem to go back the other way. If I want to transform the the current polar grid (r,th) to it's cartesian version, and interpolate onto my target cartesian grid (x,y), I get an error as follows:

[thg,rg] = meshgrid(th,r);
[xp,yp] = pol2cart(thg,rg);
[xg,yg] = meshgrid(x,y);

Z = interp2(xp,yp,Z_rth,xg,yg);

Gives ==>

Error using interp2 (line 155)
X and Y must be matrices produced by MESHGRID. Use TriScatteredInterp instead
of INTERP2 for scattered data.

I cannot figure out what is being done wrong here... xp,yp,xg,yg are all in meshgrid form... Can someone tell me what I'm missing? Thanks!

Navid

unread,
Jan 28, 2012, 1:09:10 PM1/28/12
to
Have you found a solution?
Because I have the exact same problem myself!
N


"Jeremy " <jeremy....@colorado.edu> wrote in message <j9cf4c$civ$1...@newscl01ah.mathworks.com>...

Navid

unread,
Jan 28, 2012, 1:10:10 PM1/28/12
to
Have you found a solution?
Because I have the exact same problem myself!
N


"Jeremy " <jeremy....@colorado.edu> wrote in message <j9cf4c$civ$1...@newscl01ah.mathworks.com>...

Matt J

unread,
Jan 29, 2012, 2:18:10 PM1/29/12
to
"Jeremy " <jeremy....@colorado.edu> wrote in message <j9cf4c$civ$1...@newscl01ah.mathworks.com>...
>
> [thg,rg] = meshgrid(th,r);
> [xp,yp] = pol2cart(thg,rg);
> [xg,yg] = meshgrid(x,y);
>
> Z = interp2(xp,yp,Z_rth,xg,yg);
>
> Gives ==>
>
> Error using interp2 (line 155)
> X and Y must be matrices produced by MESHGRID. Use TriScatteredInterp instead
> of INTERP2 for scattered data.
>
> I cannot figure out what is being done wrong here... xp,yp,xg,yg are all in meshgrid form... Can someone tell me what I'm missing? Thanks!
================

xp,yp is NOT in meshgrid form. Even though thg,rg are the output of meshgrid, transforming them with pol2cart does not preserve their meshed-ness.

Luca

unread,
Jun 6, 2012, 7:16:07 AM6/6/12
to
"Matt J" wrote in message <jg461i$lc0$1...@newscl01ah.mathworks.com>...

> xp,yp is NOT in meshgrid form. Even though thg,rg are the output of meshgrid, >transforming them with pol2cart does not preserve their meshed-ness.

Could you kindly suggest a way to perform the backward conversion from spherical to cartesian coordinates??

Torsten

unread,
Jun 6, 2012, 8:55:02 AM6/6/12
to
>
> Could you kindly suggest a way to perform the backward conversion from spherical to cartesian coordinates??

http://www.mathworks.fr/help/techdoc/ref/sph2cart.html
http://www.mathworks.fr/help/techdoc/ref/cart2sph.html

Best wishes
Torsten.

Luca

unread,
Jun 6, 2012, 9:53:07 AM6/6/12
to
Torsten <Torsten...@umsicht.fraunhofer.de> wrote in message
> http://www.mathworks.fr/help/techdoc/ref/sph2cart.html
> http://www.mathworks.fr/help/techdoc/ref/cart2sph.html

That was absolutely not the question of the original post!
sph2cart converts a x,y,z triplet of coordinates to a r,theta,phi triplet.
The OP (but I have the exact same problem) was asking for a way to convert a matrix of values sampled in a spherical space (at each i,j,k index of the matrix corresponds a r,theta,phi coordinate) to a cartesian space.
You would naively think to generate a uniform grid of x,y,z points, find their corresponding spherical coordinates and then do
V_cartesian = interp3 (r_start,theta_start,phi_start, V_spherical,r_cartGrid,phi_cartGrid,phi_cartGrid)

But this is not possible because:
"Error using interp3 (line 155)
X,Y,Z must be matrices produced by MESHGRID. Use TriScatteredInterp instead
of INTERP3 for scattered data."


BTW, i managed to have a working code using TriScatteredInterp but
1) I'm not sure if I TriScatteredInterp does what I really want to do.
Anyway the output is what I expect
2) It takes about 200 times what it takes for the cartesian to spherical conversion using interp3 for the same matrix sizes (on a TOP performance PC to work on a 90x90x90 matrix it takes about 80ms using interp3 for the forward conversion and 15 second for the backward conversion using TriScatteredInterp

Cheers, Luca

Matt J

unread,
Jun 6, 2012, 10:07:07 AM6/6/12
to
"Luca " <l.pres...@MOVE.campus.unimib.NOTit> wrote in message <jqnnc3$p6q$1...@newscl01ah.mathworks.com>...
> Torsten <Torsten...@umsicht.fraunhofer.de> wrote in message
> > http://www.mathworks.fr/help/techdoc/ref/sph2cart.html
> > http://www.mathworks.fr/help/techdoc/ref/cart2sph.html
>
> That was absolutely not the question of the original post!
==============

No, but it was the question in your post. The only thing you initially asked was how to do spherical-cartesian conversions. Torsten gave you just that.



> The OP (but I have the exact same problem) was asking for a way to convert a matrix of values sampled in a spherical space (at each i,j,k index of the matrix corresponds a r,theta,phi coordinate) to a cartesian space.
============

The OP was working with polar, not spherical coordinates. In any case, in what way is it still a problem for you? You have mentioned that you can do the transformation successfully with INTERP3, and in about 80ms, which seems pretty fast. INTERP3 does seem to be the appropriate solution.

Luca

unread,
Jun 6, 2012, 10:25:07 AM6/6/12
to
"Matt J" wrote in message <jqno6b$t6h$1...@newscl01ah.mathworks.com>...
>No, but it was the question in your post. The only thing you initially asked was
>how to do spherical-cartesian conversions. Torsten gave you just that.

I was asking a question referring to what you said in the previous post!
You explained what's the problem that prevents you to use interp but you did not say which was a possible solution!


> > The OP (but I have the exact same problem) was asking for a way to convert a matrix of values sampled in a spherical space (at each i,j,k index of the matrix corresponds a r,theta,phi coordinate) to a cartesian space.
> ============
>
> The OP was working with polar, not spherical coordinates. In any case, in what
>way is it still a problem for you? You have mentioned that you can do the
>transformation successfully with INTERP3, and in about 80ms, which seems pretty >fast. INTERP3 does seem to be the appropriate solution.


Polar or spherical coordinates share the exact same problem so if one can solve the problem in any of the two cases you can solve it in the other one.
As I said before I can successfully interpolate my field from cartesian to spherical coordinates using interp3 in a very short time. I cannot do the opposite from spherical to cartesian for the reason you wrote about in post #4. So in the end I figured a way (rather obscure for me actually... I'm not sure what the commands I'm using are doing) using TriScatteredInterp. But this takes 200 times what it takes to go from cartesian to spherical. And this happens even if I'm using linear interpolation on double numbers in the cartesian to sperical conversion and using nearest neighbour in TriScatteredInterp. (I would like to work on "logic" also to speed more the thing, i have to get back only a mask...but TriScatteredInterp wants only double)

Matt J

unread,
Jun 6, 2012, 10:49:07 AM6/6/12
to
"Luca " <l.pres...@MOVE.campus.unimib.NOTit> wrote in message <jqnp83$4ts$1...@newscl01ah.mathworks.com>...
>
> Polar or spherical coordinates share the exact same problem so if one can solve the problem in any of the two cases you can solve it in the other one.
> As I said before I can successfully interpolate my field from cartesian to spherical coordinates using interp3 in a very short time. I cannot do the opposite from spherical to cartesian for the reason you wrote about in post #4. So in the end I figured a way (rather obscure for me actually... I'm not sure what the commands I'm using are doing) using TriScatteredInterp. But this takes 200 times what it takes to go from cartesian to spherical. And this happens even if I'm using linear interpolation on double numbers in the cartesian to sperical conversion and using nearest neighbour in TriScatteredInterp. (I would like to work on "logic" also to speed more the thing, i have to get back only a mask...but TriScatteredInterp wants only double)
===============

Ah. Well, that's probably why I didn't give a solution in post #4. I didn't think there is one. Scattered interpolation is harder to do, in general, than gridded interpolation.

One idea that now occurs to me, though, is that you can group the spherical samples into co-planar sets of points with a common theta-coordinate (or equivalently a common z-coordinate). You could for-loop over each of these planes and transform them to 2D Cartesian samples using 2D scattered interpolation, which should be faster than 3D scattered interpolation. You will then have a series of Cartesian samples but with non-uniformly spaced z-planes. If you want the planes uniformly sampled, you can resample the Cartesian samples along the z-axis using INTERP1, which should also be pretty fast.

Luca

unread,
Jun 6, 2012, 11:15:07 AM6/6/12
to
"Matt J" wrote in message <jqnql3$b4i$1...@newscl01ah.mathworks.com>...

> Ah. Well, that's probably why I didn't give a solution in post #4. I didn't think there is one. Scattered interpolation is harder to do, in general, than gridded interpolation.
>
Too bad! It can't help but think that there should be an easy way to do this.
I think I'll just look for another way to solve my problem that does not involve a backward transformation from spherical space to cartesian one.

(and in the meantime I'll use scattered interpolation)

mohammadreza

unread,
Jun 29, 2015, 6:24:08 AM6/29/15
to
"Luca " <l.pres...@MOVE.campus.unimib.NOTit> wrote in message <jqns5r$i09$1...@newscl01ah.mathworks.com>...
Would you mind to reveal your answer, as I have a same problem.
Thanks.

Paul

unread,
Jun 30, 2015, 1:55:07 PM6/30/15
to
"mohammadreza" <ms5...@soton.ac.uk> wrote in message <mmr6c3$knf$1...@newscl01ah.mathworks.com>...

Paul

unread,
Apr 14, 2016, 8:15:20 AM4/14/16
to
"Jeremy " <jeremy....@colorado.edu> wrote in message <j9cf4c$civ$1...@newscl01ah.mathworks.com>...

Loren Shure

unread,
Apr 15, 2016, 7:01:27 AM4/15/16
to

"Paul" <ms5...@soton.ac.uk> wrote in message
news:neo1kh$ce6$1...@newscl01ah.mathworks.com...
Once you transform the data, the x and y values are not monotonic. I think
that's what the error is really pointing at.

--
--Loren

http://blogs.mathworks.com/loren

0 new messages