[Numpy-discussion] Advanced indexing: "fancy" vs. orthogonal

josef.pktd at gmail.com josef.pktd at gmail.com
Thu Apr 2 23:42:54 EDT 2015


On Thu, Apr 2, 2015 at 11:30 PM, Nathaniel Smith <njs at pobox.com> wrote:
> On Thu, Apr 2, 2015 at 6:35 PM,  <josef.pktd at gmail.com> wrote:
>> (I thought about this because I was looking at accessing off-diagonal
>> elements, m2[np.arange(4), np.arange(4) + 1] )
>
> Psst: np.diagonal(m2, offset=1)

It was just an example  (banded or toeplitz)
(I know how indexing works, kind off, but don't remember what diag or
other functions are exactly doing.)

>>> m2b = m2.copy()
>>> m2b[np.arange(4), np.arange(4) + 1]
array([  1.,   7.,  13.,  19.])
>>> m2b[np.arange(4), np.arange(4) + 1] = np.nan
>>> m2b
array([[  0.,  nan,   2.,   3.,   4.],
       [  0.,   6.,  nan,   8.,   9.],
       [  0.,   0.,  12.,  nan,  14.],
       [  0.,   0.,   0.,  18.,  nan],
       [  0.,   0.,   0.,   0.,  24.]])

>>> m2c = m2.copy()
>>> np.diagonal(m2c, offset=1) = np.nan
SyntaxError: can't assign to function call
>>> dd = np.diagonal(m2c, offset=1)
>>> dd[:] = np.nan
Traceback (most recent call last):
  File "<pyshell#89>", line 1, in <module>
    dd[:] = np.nan
ValueError: assignment destination is read-only
>>> np.__version__
'1.9.2rc1'

>>> m2d = m2.copy()
>>> m2d[np.arange(4)[::-1], np.arange(4) + 1] = np.nan

Josef

>
> --
> Nathaniel J. Smith -- http://vorpus.org
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list