Jaime Fernández del Río wrote
> On Wed, Mar 26, 2014 at 1:28 PM, Slaunger <

> Slaunger@

> > wrote:
> 
> See if you can make sense of the following. It is a little cryptic, but it
> works:
> 
> f_change = np.array([2, 3, 39, 41, 58, 59, 65, 66, 93, 102, 145])
> 
> g_change = np.array([2, 94, 101, 146, 149])
> 
> N = 150
> 
> 
> if len(f_change) % 2 :
> 
>     f_change = np.append(f_change, N)
> 
> 
> if len(g_change) % 2 :
> 
>     g_change = np.append(g_change, N)
> 
> 
> idx = np.searchsorted(f_change, g_change)
> 
> 
> f_change_exp = np.insert(np.insert(f_change, idx, g_change),
> 
>                          idx + np.arange(len(idx)), g_change)
> 
> 
> idx2 = np.searchsorted(g_change, f_change_exp)
> 
> 
> f_change_lens = f_change_exp[1::2] - f_change_exp[::2]
> 
> true_true_intervals = idx2[1::2] % 2 != 0
> 
> 
> total = np.sum(f_change_lens[true_true_intervals])
> 
> 
>>>> total
> 
> 9
> 
> I'll gladly elaborate on what's going on, just ask!
> 
> Jaime

Holá Jaime!

YOU ARE A GENIUS!. I understand exactly what you are doing!

You know what. I just had a shower, nothing like having a shower for
thinking about hard problems, and I got the same idea: Make the changes_at
arrays even length if needed by appending N, then use searchsorted to figure
out what values to merge and where!

Only I did not know about the append and insert methods. Very, very nice! (I
only knew concatenate, which would be clumsy for just appending one
element), and for the insert I would have concatenated and in-place sorted,
but your solution is much more elegant!

You saved my evening! Actually, my head has been spinning about this problem
the last three evenings without having been able to nail it down.

Abrazos de Dinamarca,

Slaunger




--
View this message in context: 
http://numpy-discussion.10968.n7.nabble.com/Is-there-a-pure-numpy-recipe-for-this-tp37077p37087.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to