Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 18th, 2012  |  syntax: None  |  size: 0.91 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. In numpy, how can I assign an array of size N to a larger array with a boolean mask
  2. a = np.array([(0,0,0),
  3.        (1,0,0),
  4.        (2,1,0),
  5.        (3,1,0),
  6.        (4,1,0),
  7.        (5,0,0),
  8.        (6,0,0),
  9.        (7,0,0),
  10.        (8,1,0),
  11.        (9,1,0)],
  12.        dtype=np.dtype([('time', '<i4'), ('ena', '|b1'), ('elapsed', '<i4')]))
  13.        
  14. elapsed =  a[a['ena']]['timestamp'][1:] - a[a['ena']]['timestamp'][0:-1]
  15.        
  16. a[a['ena']]['step_secs'][1:] = timestep
  17.        
  18. a = np.array([
  19.        (0,0,0),
  20.        (1,0,0),
  21.        (2,1,0),
  22.        (3,1,1),
  23.        (4,1,1),
  24.        (5,0,0),
  25.        (6,0,0),
  26.        (7,0,0),
  27.        (8,1,4),
  28.        (9,1,1)]
  29.        
  30. >>> a = np.zeros(3)
  31. >>> b = np.array([True, False, True])
  32. >>> a[b][1:] = 2
  33. array([ 0.,  0.,  0.])
  34. >>> a[1:][b[1:]] = 2
  35. array([ 0.,  0.,  2.])
  36.        
  37. a[a['ena']]['step_secs'][1:] = timestep
  38.        
  39. tmp = a['ena'][1:]
  40. a['step_secs'][1:][tmp] = timestep
  41.        
  42. a['step_secs'][1:][a['ena'][1:]] = timestep