How to fix the diagonal values of a matrix
How can i take out the diagonal values of a matrix and fix them to zero? Example: input: [[2,3,4],[3,4,5],[4,5,6]] output: [[0,3,4],[3,0,5],[4,5,0]] Thanks. -- View this message in context: http://old.nabble.com/How-to-fix-the-diagonal-values-of-a-matrix-tp27917991p... Sent from the Numpy-discussion mailing list archive at Nabble.com.
On Tue, Mar 16, 2010 at 9:43 AM, gerardo.berbeglia
How can i take out the diagonal values of a matrix and fix them to zero?
Example:
input: [[2,3,4],[3,4,5],[4,5,6]]
output: [[0,3,4],[3,0,5],[4,5,0]]
assuming a is square a[range(len(a)),range(len(a))] = 0 see also np.diag Josef
Thanks. -- View this message in context: http://old.nabble.com/How-to-fix-the-diagonal-values-of-a-matrix-tp27917991p... Sent from the Numpy-discussion mailing list archive at Nabble.com.
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
On Tue, Mar 16, 2010 at 5:56 AM,
On Tue, Mar 16, 2010 at 9:43 AM, gerardo.berbeglia
wrote: How can i take out the diagonal values of a matrix and fix them to zero?
Example:
input: [[2,3,4],[3,4,5],[4,5,6]]
output: [[0,3,4],[3,0,5],[4,5,0]]
assuming a is square
a[range(len(a)),range(len(a))] = 0
see also np.diag
Josef
Or, if you need speed, here's the fast way: a.flat[::4] = 0 or more generally a.flat[::a.shape[0]+1] = 0
On Tue, Mar 16, 2010 at 7:43 AM, Keith Goodman
On Tue, Mar 16, 2010 at 5:56 AM,
wrote: On Tue, Mar 16, 2010 at 9:43 AM, gerardo.berbeglia
wrote: How can i take out the diagonal values of a matrix and fix them to zero?
Example:
input: [[2,3,4],[3,4,5],[4,5,6]]
output: [[0,3,4],[3,0,5],[4,5,0]]
assuming a is square
a[range(len(a)),range(len(a))] = 0
see also np.diag
Josef
Or, if you need speed, here's the fast way:
a.flat[::4] = 0
or more generally
a.flat[::a.shape[0]+1] = 0
Oh, I see that fill_diagonal is in numpy 1.4. So:
a = np.array([[2,3,4],[3,4,5],[4,5,6]]) np.fill_diagonal(a, 0) a
array([[0, 3, 4], [3, 0, 5], [4, 5, 0]])
On Tue, Mar 16, 2010 at 1:57 PM, Keith Goodman
On Tue, Mar 16, 2010 at 7:43 AM, Keith Goodman
wrote: On Tue, Mar 16, 2010 at 5:56 AM,
wrote: On Tue, Mar 16, 2010 at 9:43 AM, gerardo.berbeglia
wrote: How can i take out the diagonal values of a matrix and fix them to zero?
Example:
input: [[2,3,4],[3,4,5],[4,5,6]]
output: [[0,3,4],[3,0,5],[4,5,0]]
assuming a is square
a[range(len(a)),range(len(a))] = 0
see also np.diag
Josef
Or, if you need speed, here's the fast way:
a.flat[::4] = 0
or more generally
a.flat[::a.shape[0]+1] = 0
Oh, I see that fill_diagonal is in numpy 1.4. So:
a = np.array([[2,3,4],[3,4,5],[4,5,6]]) np.fill_diagonal(a, 0) a
array([[0, 3, 4], [3, 0, 5], [4, 5, 0]])
it looks like there are a lot of functions in numpy np.source(np.fill_diagonal) shows it's the same as your previous version this (seems to) work for non-square nd arrays with ndim>1:
a = np.ones((2,3,4)) a[[range(min(a.shape))]*a.ndim] = 0 a array([[[ 0., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]],
[[ 1., 1., 1., 1.], [ 1., 0., 1., 1.], [ 1., 1., 1., 1.]]])
a = np.ones((4,5)) a[[range(min(a.shape))]*a.ndim] = 0 a array([[ 0., 1., 1., 1., 1.], [ 1., 0., 1., 1., 1.], [ 1., 1., 0., 1., 1.], [ 1., 1., 1., 0., 1.]])
Josef
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (3)
-
gerardo.berbeglia
-
josef.pktd@gmail.com
-
Keith Goodman