# [Numpy-discussion] How to remove any row or column of a numpy matrix whose sum is 3?

Chris Barker chris.barker at noaa.gov
Mon Jun 4 12:51:10 EDT 2012

```On Mon, Jun 4, 2012 at 9:21 AM, bob tnur <bobtnur78 at gmail.com> wrote:
> Hello every body. I am new to python.
> How to remove any row or column of a numpy matrix whose sum is 3.
> To obtain and save new matrix P with (sum(anyrow)!=3 and sum(anycolumn)!=3
> elements.

well, one question is -- do you want to remove the particular rows
first, then remove the particular columns, or compute the sums of both
with all in place, then remove rows and columns -- but some ideas:

In : m
Out:
array([[ 1.,  1.,  1.,  1.,  0.],
[ 1.,  0.,  1.,  1.,  1.],
[ 1.,  1.,  1.,  0.,  0.],
[ 1.,  1.,  1.,  1.,  1.]])

# which rows, columns sum to 3?

In : rows_3 = np.argwhere(m.sum(axis=1) == 3)

In : rows_3
Out: array([])

In : cols_3 = np.argwhere(m.sum(axis=0) == 3)

In : cols_3
Out:
array([,
])

# but it's probably easier to know which do not sum to 3:

In : rows_3 = np.argwhere(m.sum(axis=1) != 3)

In : rows_3
Out:
array([,
,
])

In : cols_3 = np.argwhere(m.sum(axis=0) != 3)

In : cols_3
Out:
array([,
,
])

now build the new array:

m2 = In : m2 = m[rows_3[:,0]][:, cols_3[:,0]]

In : m2
Out:
array([[ 1.,  1.,  0.],
[ 1.,  1.,  1.],
[ 1.,  1.,  1.]])

some trickery there:

the [:,2] index is because argwhere() returns a 2-d (nXm)array of
indexes -- where m is the rank of the input array -- in this case one,
so we want the single column (it's done this way so you can do:

arr[ argwhere(arr == something) ]

for n-dim arrays

I also found I need to pull out the rows first, then the columns, because:

arr[a_1, a_2]

is interpreted as two arrays of individual indexes, not as indexes to
the roes an columsn, i.e.:

In : arr
Out:
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])

In : arr[(1,2), (2,3)]
Out: array([ 6, 11])

you got the elements at: (1,2) and (2,3)

There may be a way to do that a bit cleaner -- it escapes me at the moment.

-Chris

--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

```