
Untitled
By: a guest on
Apr 18th, 2012 | syntax:
None | size: 0.91 KB | hits: 11 | expires: Never
In numpy, how can I assign an array of size N to a larger array with a boolean mask
a = np.array([(0,0,0),
(1,0,0),
(2,1,0),
(3,1,0),
(4,1,0),
(5,0,0),
(6,0,0),
(7,0,0),
(8,1,0),
(9,1,0)],
dtype=np.dtype([('time', '<i4'), ('ena', '|b1'), ('elapsed', '<i4')]))
elapsed = a[a['ena']]['timestamp'][1:] - a[a['ena']]['timestamp'][0:-1]
a[a['ena']]['step_secs'][1:] = timestep
a = np.array([
(0,0,0),
(1,0,0),
(2,1,0),
(3,1,1),
(4,1,1),
(5,0,0),
(6,0,0),
(7,0,0),
(8,1,4),
(9,1,1)]
>>> a = np.zeros(3)
>>> b = np.array([True, False, True])
>>> a[b][1:] = 2
array([ 0., 0., 0.])
>>> a[1:][b[1:]] = 2
array([ 0., 0., 2.])
a[a['ena']]['step_secs'][1:] = timestep
tmp = a['ena'][1:]
a['step_secs'][1:][tmp] = timestep
a['step_secs'][1:][a['ena'][1:]] = timestep