[Numpy-discussion] How to fix the diagonal values of a matrix

josef.pktd@gmai... josef.pktd@gmai...
Tue Mar 16 08:56:45 CDT 2010


On Tue, Mar 16, 2010 at 9:43 AM, gerardo.berbeglia <gberbeglia@gmail.com> 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

>
> Thanks.
> --
> View this message in context: http://old.nabble.com/How-to-fix-the-diagonal-values-of-a-matrix-tp27917991p27917991.html
> 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
>


More information about the NumPy-Discussion mailing list