Skip to content

Instantly share code, notes, and snippets.

@mynameisfiber
Created January 19, 2012 20:31
Show Gist options
  • Save mynameisfiber/1642403 to your computer and use it in GitHub Desktop.
Save mynameisfiber/1642403 to your computer and use it in GitHub Desktop.
Numpy "masking" on index
In [21]: a = np.zeros( (20) )
In [22]: b = np.asarray( [2, 5, 7, 15] )
In [23]: a
Out[23]:
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0.])
In [24]: a[b] = 1
In [25]: a
Out[25]:
array([ 0., 0., 1., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0.,
0., 0., 1., 0., 0., 0., 0.])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment