Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Two assumptions: 1: You are not looking for the nearest neighbour, but for all altitudes within some range. So, let's say for a1 you want all bn that are within 0.5 of a1 (giving you b1 and b2 as per your example). I would define the 'nearest neighbour' as something different.</p> <p>2: You don't count nan in your medians (numpy counts them as infinity as per some IEEE convention, but this seems odd to me). As per your suggestion we thus use nanmedian from scipy.stats.</p> <p>I would do the following:</p> <pre><code>from numpy import * from pylab import * A_Alt = array([1,2,3,4]) A_Val = array([.33, .5, .6, .8]) B_Alt = array([.7, 0.9, 1.7, 2., 2.4, 2.9, 3.1, 3.2, 3.9, 4.1]) B_Val = array([.3, NaN, .8, .6, .7, .4, .3, NaN, .99, 1.3]) range = .5 B_Agrid = [nanmedian(B_Val[abs(B_Alt - k)&lt;range]).item() for k in A_Alt] A_Bgrid = [nanmedian(A_Val[abs(A_Alt - k)&lt;range]).item() for k in B_Alt] </code></pre> <p>We find all indices where the distance of B_Alt to k in A_Alt is less than a specified range. Then we take the median of those B_Val. The same works for A_Bgrid with the results as requested.</p> <p>==Edit==</p> <p>Different assumption as to your nearest neighbours: Let's take the nearest neighbour to be the entry (or entries in case of a tie) with the smallest absolute altitude difference while not having nan as a value. N.B. these results do not match your example as b1 would not be the nearest neighbour of a1 on account of b2 being closer.</p> <p>Under this assumption, the following code should work:</p> <pre><code>from numpy import * from pylab import * from scipy.stats import nanmedian A_Alt = array([1,2,3,4]) A_Val = array([.33, .5, .6, .8]) B_Alt = array([.7, 0.9, 1.7, 2., 2.4, 2.9, 3.1, 3.2, 3.9, 4.1]) B_Val = array([.3, NaN, .8, .6, .7, .4, .3, NaN, .99, 1.3]) def ReGridMedian(AltIn, ValIn, AltOut): part = isfinite(ValIn) q = [abs(AltIn[part]-k) for k in AltOut] q = [nonzero(abs(k - min(k))&lt;3*finfo(k.dtype).eps) for k in q] q = [ValIn[part][k] for k in q] return [median(k) for k in q] B_Agrid = ReGridMedian(B_Alt, B_Val, A_Alt) A_Bgrid = ReGridMedian(A_Alt, A_Val, B_Alt) </code></pre> <p>I hacked together something that checks if two values are identical within machine precision, but I assume there's a better way of doing that. In any case, we first filter all values that are not nan, then find the closest match, then check for duplicate minima, then get the median of those values.</p> <p>====</p> <p>Does this cover your question, or are my assumptions incorrect?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload