incrementing along a diagonal

David Novakovic david at distip.com.au
Wed Oct 11 19:20:29 CDT 2006


Thanks for the help, i've learnt a lot and also figured out something
that does what I want,  i'll paste  an interactive session below:

 x = zeros((4,7))
 x
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
 index = arange(min(x.shape[0], x.shape[1]))
 index2 = copy.deepcopy(index) #deep copy may be overkill
 for a,b in enumerate(index):
...   index2[a] += a
...
 if len(x[:,0]) > len(x[0]):
...   x[index2,index] +=1
... else:
...   x[index,index2] +=1
...
 x
array([[1, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 1]])


Thanks for the tips

Dave Novakovic

P.S. subscribed to the list now

Bill Baxter wrote:
> Forgot to CC you...
>
> ---------- Forwarded message ----------
> From: Bill Baxter <wbaxter at gmail.com>
> Date: Oct 12, 2006 8:58 AM
> Subject: Re: [Numpy-discussion] incrementing along a diagonal
> To: Discussion of Numerical Python
> <numpy-discussion at lists.sourceforge.net>
>
>
> 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
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list