0

I'm trying to return a numpy flattened array of a numpy matrix where all the values where the row == col is ignored.

For example:

>>> m = numpy.matrix([[1,2,3],[4,5,6],[7,8,9]])
>>> m
matrix([[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]])

# Some function....

# result:

m_flat = array([2,3,4,6,7,8])
Alex McLean
  • 2,524
  • 5
  • 30
  • 53

1 Answers1

3

You could use np.eye to create the appropriate boolean mask:

In [139]: np.eye(m.shape[0], dtype='bool')
Out[139]: 
array([[ True, False, False],
       [False,  True, False],
       [False, False,  True]], dtype=bool)

In [140]: m[~np.eye(m.shape[0], dtype='bool')]
Out[140]: matrix([[2, 3, 4, 6, 7, 8]])
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677