incrementing along a diagonal

Johannes Loehnert a.u.r.e.l.i.a.n at
Thu Oct 12 01:40:49 CDT 2006


I absolutely do not know perl, so I do not know what the expression you posted 

However, the key is just to understand indexing in numpy:

if you have a matrix mat and index arrays index1, index2 with, lets say,

index1 = array([ 17, 19, 29])
index2 = array([ 12, 3,  9])

then the entries of the index arrays are used as row and column indices 
respectively, and the result will be an array shaped like the index arrays.

So doing

mat[index1, index2]

will give you

--> array([ mat[17, 12], mat[19, 3], mat[29, 9]]).

Now if you want the diagonal of a 3x3-mat, you need 

index1=index2=array([ 0, 1, 2]).
mat[index1, index2] --> array([ mat[0,0], mat[1,1], mat[2,2]])

That is what my code does. If you need other, arbitrary subsets of mat, you 
just have to fill the index arrays accordingly.


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
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list