Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The second of the following two little functions can get what you want done:</p> <pre><code>def resample_up(t, x, r) : return x[np.argmax(r[:, None] &lt;= t, axis=1)] def resample_down(t, x, r) : return x[::-1][np.argmax(r[:, None] &gt;= t[::-1], axis=1)] &gt;&gt;&gt; resample_up(t, x, r) array([10, 12, 13, 14]) &gt;&gt;&gt; resample_down(t, x, r) array([10, 11, 12, 14]) </code></pre> <p>If you find it hard to figure out what's going on, the following may help:</p> <pre><code>&gt;&gt;&gt; r[:, None] &lt;= t array([[ True, True, True, True, True], [False, False, True, True, True], [False, False, False, True, True], [False, False, False, False, True]], dtype=bool) &gt;&gt;&gt; r[:, None] &gt;= t[::-1] array([[False, False, False, False, True], [False, False, False, True, True], [False, False, True, True, True], [ True, True, True, True, True]], dtype=bool) </code></pre> <p>and then <code>np.argmax</code> is returning the index of the first occurrence of <code>True</code> in every row.</p> <p><strong>EDIT</strong> IT is hard to get it shorter than a single line of code, but for large arrays performance will suffer, because the index finding never breaks out early of loops. So for very large arrays, scanning through the array with python loops may turn out to be faster. For smaller ones it does not:</p> <pre><code>In [2]: %timeit resample_up(t, x, r) 100000 loops, best of 3: 7.32 us per loop In [3]: %timeit resample_down(t, x, r) 100000 loops, best of 3: 8.44 us per loop In [4]: %timeit resample(t, x, r) # modified version of the OP's taking also x 100000 loops, best of 3: 13.7 us per loop </code></pre>
    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. 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