From: Travis O. <oli...@ie...> - 2006-10-12 02:43:40
|
David Novakovic wrote: > Hi, > > i'm moving some old perl PDL code to python. I've come across a line > which changes values in a diagonal line accross a matrix. > > matrix.diagonal() returns a list of values, but making changes to these > does not reflect in the original (naturally). > > I'm just wondering if there is a way that i can increment all the values > along a diagonal? > You can refer to a diagonal using flattened index with a element-skip equal to the number of columns+1. Thus, a.flat[::a.shape[1]+1] += 1 will increment the elements of a along the main diagonal. -Travis |