Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Error when finding nearest neighbour on longitude/latitude (irregular

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Charlie

Date: 2 Mar, 2012 17:47:02

Message: 1 of 11

Hello everyone.

I have a fairly simple problem I am working on but can't seem to figure out:

I have some data from a forecasting system that has longitude (named: nav_lon), latitude (nav_lat), and then a value of temperature at each point (temp).

The grid itself is irregular and so the dimensions of the longitude are 2D: 257x357 (and the same for latitude).

To compare with these model results, I have a matrix of observations taken all over the world (which are scattered point measurements, not on a grid), this is much simpler data, a single 1-D column of longitude, a single 1-D column of latitude, and then a corresponding temperature for those observations.

I would like to loop through each observation point and find the corresponding point from our data (this could be the nearest neighbor point, or some form of interpolation, in fact I will probably like to eventually include both options for testing) - I can then perform some stats to see how well we are doing.

Seeing as how there could be 100k+ observations, I am trying to find the fastest method of doing this.

I was reading that DelauneyTri was a good way to tackle this problem, but it seems to complain about my longitude and latitude being 2-dimensional and I get this error:

tri = DelaunayTri(nav_lon,nav_lat);
??? Error using ==> DelaunayTri
Invalid data format for DelaunayTri.

could someone suggest a good way to tackle this problem?

Thanks

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Roger Stafford

Date: 2 Mar, 2012 23:53:35

Message: 2 of 11

Charlie <charliebishop@gmail.com> wrote in message <16108795.1311.1330710422622.JavaMail.geo-discussion-forums@vbbed8>...
> could someone suggest a good way to tackle this problem?
- - - - - - - -
  With a couple of calls on 'histc' allowing quick location of your various observation longitude-latitude coordinates in the appropriate model longitude-latitude rectangles, you should be able to do your own 2D interpolation of model temperatures using, say, a simple bilinear interpolation, and thereby make a comparison with observation temperatures. It shouldn't be hard to do.

  If T11 is the model temperature at (x1,y1), T12 that at (x1,y2), T21 at (x2,y1), and T22 at (x2,y2), then the bilinear interpolated value at an (x,y) point within that rectangle would be:

 (T11*(x2-x)*(y2-y)+T12*(x2-x)*(y-y1)+T21*(x-x1)*(y2-y)+T22*(x-x1)*(y-y1)) ...
   / ((x2-x1)*(y2-y1));

where x and y denote longitude and latitude values. You would presumably be comparing this with your corresponding observed temperature value at (x,y).

  Unless I am mistaken, the whole thing could be vectorized without any for-loops, and would probably execute fairly rapidly.

Roger Stafford

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Bruno Luong

Date: 3 Mar, 2012 07:46:17

Message: 3 of 11

Charlie <charliebishop@gmail.com> wrote in message
> tri = DelaunayTri(nav_lon,nav_lat);
> ??? Error using ==> DelaunayTri
> Invalid data format for DelaunayTri.

tri = DelaunayTri(nav_lon(:),nav_lat(:))

You should read the help of DelaunayTri, your data is (m x n) but scattered so consider it as (m*n x 1).

Bruno

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Charlie

Date: 14 Mar, 2012 19:23:18

Message: 4 of 11

On Saturday, 3 March 2012 04:16:17 UTC-3:30, Bruno Luong wrote:
> Charlie wrote in message
> > tri = DelaunayTri(nav_lon,nav_lat);
> > ??? Error using ==> DelaunayTri
> > Invalid data format for DelaunayTri.
>
> tri = DelaunayTri(nav_lon(:),nav_lat(:))
>
> You should read the help of DelaunayTri, your data is (m x n) but scattered so consider it as (m*n x 1).
>
> Bruno



On Saturday, 3 March 2012 04:16:17 UTC-3:30, Bruno Luong wrote:
> Charlie wrote in message
> > tri = DelaunayTri(nav_lon,nav_lat);
> > ??? Error using ==> DelaunayTri
> > Invalid data format for DelaunayTri.
>
> tri = DelaunayTri(nav_lon(:),nav_lat(:))
>
> You should read the help of DelaunayTri, your data is (m x n) but scattered so consider it as (m*n x 1).
>
> Bruno

Hi Bruno,

Thanks for addressing this, it worked perfectly. Is there a way I can extend this into the vertical dimension as well? I have a depth vector that is 50 entries long.. not sure how I would extend it to 3 coords?

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Charlie

Date: 14 Mar, 2012 19:18:24

Message: 5 of 11

On Saturday, 3 March 2012 04:16:17 UTC-3:30, Bruno Luong wrote:
> Charlie wrote in message
> > tri = DelaunayTri(nav_lon,nav_lat);
> > ??? Error using ==> DelaunayTri
> > Invalid data format for DelaunayTri.
>
> tri = DelaunayTri(nav_lon(:),nav_lat(:))
>
> You should read the help of DelaunayTri, your data is (m x n) but scattered so consider it as (m*n x 1).
>
> Bruno

Hi Bruno,

Thanks for addressing this, it worked perfectly. Is there a way I can extend this into the vertical dimension as well? I have a depth vector that is 50 entries long.. not sure how I would extend it to 3 coords?

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Bruno Luong

Date: 15 Mar, 2012 12:59:17

Message: 6 of 11

Charlie <charliebishop@gmail.com> wrote in message
> Thanks for addressing this, it worked perfectly. Is there a way I can extend this into the vertical dimension as well? I have a depth vector that is 50 entries long.. not sure how I would extend it to 3 coords?

DelaunayTri supports 2d and 3d. So I don't see any problem here.

Bruno

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Charlie

Date: 15 Mar, 2012 13:30:10

Message: 7 of 11

On Thursday, 15 March 2012 10:29:17 UTC-2:30, Bruno Luong wrote:
> Charlie <charliebishop@gmail.com> wrote in message
> > Thanks for addressing this, it worked perfectly. Is there a way I can extend this into the vertical dimension as well? I have a depth vector that is 50 entries long.. not sure how I would extend it to 3 coords?
>
> DelaunayTri supports 2d and 3d. So I don't see any problem here.
>
> Bruno

I thought so to...

If I do: trigrid = DelaunayTri(nav_lon(:),nav_lat(:)) , it works fine..

However if I try :

trigrid = DelaunayTri(nav_lon(:),nav_lat(:),deptht); OR

trigrid = DelaunayTri(nav_lon(:),nav_lat(:),deptht(:));

I get the error: ??? Error using ==> DelaunayTri
Data point coordinates have inconsistent dimension.

like I mentioned before, my nav_lon and nav_lat are 257x357 .... however my deptht variable is only 1 column with 50 entries (corresponding to 50 depth levels)

Maybe I need to reshape some of my variables? the temperature data that I will be interpolating is 257x357x50 ... I tried making nav_lon and nav_lat 257x357x50 (essentially duplicating itself 50 times) but this didnt work for me either...

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Steven_Lord

Date: 15 Mar, 2012 13:57:48

Message: 8 of 11



"Charlie" <charliebishop@gmail.com> wrote in message
news:15039096.28.1331818210365.JavaMail.geo-discussion-forums@vblo18...
> On Thursday, 15 March 2012 10:29:17 UTC-2:30, Bruno Luong wrote:
>> Charlie <charliebishop@gmail.com> wrote in message
>> > Thanks for addressing this, it worked perfectly. Is there a way I can
>> > extend this into the vertical dimension as well? I have a depth vector
>> > that is 50 entries long.. not sure how I would extend it to 3 coords?
>>
>> DelaunayTri supports 2d and 3d. So I don't see any problem here.
>>
>> Bruno
>
> I thought so to...
>
> If I do: trigrid = DelaunayTri(nav_lon(:),nav_lat(:)) , it works fine..
>
> However if I try :
>
> trigrid = DelaunayTri(nav_lon(:),nav_lat(:),deptht); OR
>
> trigrid = DelaunayTri(nav_lon(:),nav_lat(:),deptht(:));
>
> I get the error: ??? Error using ==> DelaunayTri
> Data point coordinates have inconsistent dimension.
>
> like I mentioned before, my nav_lon and nav_lat are 257x357 .... however
> my deptht variable is only 1 column with 50 entries (corresponding to 50
> depth levels)

So use TriScatteredInterp instead of DelaunayTri and only specify the fifty
latitude/longitude values corresponding to your fifty data measurements.

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

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Charlie

Date: 15 Mar, 2012 14:20:32

Message: 9 of 11

On Thursday, 15 March 2012 11:27:48 UTC-2:30, Steven_Lord wrote:
> "Charlie" <charliebishop@gmail.com> wrote in message
> news:15039096.28.1331818210365.JavaMail.geo-discussion-forums@vblo18...
> > On Thursday, 15 March 2012 10:29:17 UTC-2:30, Bruno Luong wrote:
> >> Charlie <charliebishop@gmail.com> wrote in message
> >> > Thanks for addressing this, it worked perfectly. Is there a way I can
> >> > extend this into the vertical dimension as well? I have a depth vector
> >> > that is 50 entries long.. not sure how I would extend it to 3 coords?
> >>
> >> DelaunayTri supports 2d and 3d. So I don't see any problem here.
> >>
> >> Bruno
> >
> > I thought so to...
> >
> > If I do: trigrid = DelaunayTri(nav_lon(:),nav_lat(:)) , it works fine..
> >
> > However if I try :
> >
> > trigrid = DelaunayTri(nav_lon(:),nav_lat(:),deptht); OR
> >
> > trigrid = DelaunayTri(nav_lon(:),nav_lat(:),deptht(:));
> >
> > I get the error: ??? Error using ==> DelaunayTri
> > Data point coordinates have inconsistent dimension.
> >
> > like I mentioned before, my nav_lon and nav_lat are 257x357 .... however
> > my deptht variable is only 1 column with 50 entries (corresponding to 50
> > depth levels)
>
> So use TriScatteredInterp instead of DelaunayTri and only specify the fifty
> latitude/longitude values corresponding to your fifty data measurements.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Hi Steve, I think you are misinterpreting what I'm saying.

there are 257x357 x 50 levels of data points (not 50 measurements in total) ... think of a 3d box of the ocean, Lat,Lon,depth, and a temperature value at each point.

I then have a bunch of a lat,lon,depth values not on this grid (its scattered data), that I want to interpolate to so I can compare my model to these observations... DelauneyTri seems to handle my 2d case ok, but not the 3d case.

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Bruno Luong

Date: 15 Mar, 2012 16:06:22

Message: 10 of 11

Charlie <charliebishop@gmail.com> wrote in message <23384004.1311.1331821232168.JavaMail.geo-discussion-forums@vbmf37>...

>
> I then have a bunch of a lat,lon,depth values not on this grid (its scattered data), that I want to interpolate to so I can compare my model to these observations... DelauneyTri seems to handle my 2d case ok, but not the 3d case.
>

Formatting your data, just duplicate the z data with REPMAT before calling DelaunayTri.

Bruno

Subject: Error when finding nearest neighbour on longitude/latitude (irregular

From: Charlie

Date: 15 Mar, 2012 16:55:58

Message: 11 of 11

On Thursday, 15 March 2012 13:36:22 UTC-2:30, Bruno Luong wrote:
> Charlie <charliebishop@gmail.com> wrote in message <23384004.1311.1331821232168.JavaMail.geo-discussion-forums@vbmf37>...
>
> >
> > I then have a bunch of a lat,lon,depth values not on this grid (its scattered data), that I want to interpolate to so I can compare my model to these observations... DelauneyTri seems to handle my 2d case ok, but not the 3d case.
> >
>
> Formatting your data, just duplicate the z data with REPMAT before calling DelaunayTri.
>
> Bruno

Thanks, I was kind of thinking this was the way to go.. and I had never seen that repmat command before, looks interesting.

I guess I'm not really sure what size I actually need to make my matrix?

Nav_lon is 257x357, the same for nav_lat, and depths is just 1 column vector.. I tried reproducing that depth column vector to be 357 column vectors of the same depths, but that didnt work either... should it be 257x357x50 perhaps? with each vertical column being my 50 depths? ... hmmmm

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us