# incrementing along a diagonal

Bill Baxter wbaxter at gmail.com
Wed Oct 11 18:58:45 CDT 2006

```On 10/12/06, David Novakovic <david at distip.com.au> wrote:
> Johannes Loehnert wrote:
> This is very nice, exactly what i want, but it doesnt work for mxn
> matricies:
>
> >>> x = zeros((5,3))
> >>> x
> array([[0, 0, 0],
>        [0, 0, 0],
>        [0, 0, 0],
>        [0, 0, 0],
>        [0, 0, 0]])
> >>> index = arange(min(x.shape[0],x.shape[1]))
> >>> x[index,index] += 1
> >>> x
> array([[1, 0, 0],
>        [0, 1, 0],
>        [0, 0, 1],
>        [0, 0, 0],
>        [0, 0, 0]])

Exactly what output are you expecting?  That is the definition of the
'diagonal' for a non-square matrix.  If you're expecting something
else then what you want is not the diagonal.

> Just for reference, this is the line of perl i'm trying to port:
>
>
> like:
>
> for index in diag_iter(matrix,*axes):
>     matrix[index] +=1

That's not going to change the mathematical definition of the diagonal
of a non-square matrix.

> PS: If anyone would care to link me to the subscription page for the
> mailing list so you dont have to CC me all the time :)

Check the bottom of this message.

> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
>

--bb

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier