|
Re: locating the closest values? [message #80671 is a reply to message #80658] |
Tue, 26 June 2012 18:35  |
Russell Ryan
Messages: 122 Registered: May 2012
|
Senior Member |
|
|
On Tuesday, June 26, 2012 5:28:56 PM UTC-4, Mats Löfdahl wrote:
> Den tisdagen den 26:e juni 2012 kl. 22:55:34 UTC+2 skrev anil:
>> Hi,
>> I have two datasets/files. 1st one contains the values i want to find
>> (latitudes and longitudes) and the second file contains 3 columns as
>> longitude latitude and depth.
>> What i want to do is to read my desired values from file 1 and find
>> the closest values in file 2 and the corresponding 3rd column(depth)
>> from file 2. here is what i did:
>> 1st file:
>> 42.25 30.57
>> 41.17 32.58
>> ......
>> 2nd file
>> 42.55 34.42 1000
>> 41.54 33.21 1500
>> .......
>> what i did is :
>>
>> difference1=abs(lon-desiredlon)
>> a1=min(difference1,index1)
>>
>> difference2=abs(lat-desiredlat)
>> a2=min(difference2,index2)
>>
>> in this case, lon(index1) and lat(index2) become my desired values. no
>> problem with that. but how do i get the 3rd column. i could not match
>> index1 and index2 with where function.
>> what am i missing here?
>
> Index1 and index2 are different because being close in one of lat and lon is not the same as being closest in both. I suspect what you want is something like a=min(sqrt(difference1^2+difference2^2),index), which should give you the index to the point with the shortest distance from the desired point. (You may want to scale the differences to length units first.)
So, that's going to work, but the distance you get "a" is probably not the minimum distance since you're working with angles. That said, you should look up the relevant equation for "great circle" distances to get the true minimum.
|
|
|
|
Re: locating the closest values? [message #80678 is a reply to message #80676] |
Tue, 26 June 2012 14:50  |
anil
Messages: 34 Registered: August 2009
|
Member |
|
|
On Jun 27, 12:28 am, Mats Löfdahl <mats.lofd...@gmail.com> wrote:
> Den tisdagen den 26:e juni 2012 kl. 22:55:34 UTC+2 skrev anil:
>
>
>
>
>
>
>
>
>
>> Hi,
>> I have two datasets/files. 1st one contains the values i want to find
>> (latitudes and longitudes) and the second file contains 3 columns as
>> longitude latitude and depth.
>> What i want to do is to read my desired values from file 1 and find
>> the closest values in file 2 and the corresponding 3rd column(depth)
>> from file 2. here is what i did:
>> 1st file:
>> 42.25 30.57
>> 41.17 32.58
>> ......
>> 2nd file
>> 42.55 34.42 1000
>> 41.54 33.21 1500
>> .......
>> what i did is :
>
>> difference1=abs(lon-desiredlon)
>> a1=min(difference1,index1)
>
>> difference2=abs(lat-desiredlat)
>> a2=min(difference2,index2)
>
>> in this case, lon(index1) and lat(index2) become my desired values. no
>> problem with that. but how do i get the 3rd column. i could not match
>> index1 and index2 with where function.
>> what am i missing here?
>
> Index1 and index2 are different because being close in one of lat and lon is not the same as being closest in both. I suspect what you want is something like a=min(sqrt(difference1^2+difference2^2),index), which should give you the index to the point with the shortest distance from the desired point. (You may want to scale the differences to length units first.)
Thank you for your answer. I guess I just found it. it is as simple as
it is: where function :) :
c=where(lon eq lon(index1) and lat eq lat(index2))
this c is what i want. just the statement giving the exact locations.
|
|
|
Re: locating the closest values? [message #80680 is a reply to message #80678] |
Tue, 26 June 2012 14:28  |
Mats Löfdahl
Messages: 263 Registered: January 2012
|
Senior Member |
|
|
Den tisdagen den 26:e juni 2012 kl. 22:55:34 UTC+2 skrev anil:
> Hi,
> I have two datasets/files. 1st one contains the values i want to find
> (latitudes and longitudes) and the second file contains 3 columns as
> longitude latitude and depth.
> What i want to do is to read my desired values from file 1 and find
> the closest values in file 2 and the corresponding 3rd column(depth)
> from file 2. here is what i did:
> 1st file:
> 42.25 30.57
> 41.17 32.58
> ......
> 2nd file
> 42.55 34.42 1000
> 41.54 33.21 1500
> .......
> what i did is :
>
> difference1=abs(lon-desiredlon)
> a1=min(difference1,index1)
>
> difference2=abs(lat-desiredlat)
> a2=min(difference2,index2)
>
> in this case, lon(index1) and lat(index2) become my desired values. no
> problem with that. but how do i get the 3rd column. i could not match
> index1 and index2 with where function.
> what am i missing here?
Index1 and index2 are different because being close in one of lat and lon is not the same as being closest in both. I suspect what you want is something like a=min(sqrt(difference1^2+difference2^2),index), which should give you the index to the point with the shortest distance from the desired point. (You may want to scale the differences to length units first.)
|
|
|