[Numpy-discussion] Functions for indexing into certain parts of an array (2d)
Sat Jun 6 17:02:44 CDT 2009
On Sat, Jun 6, 2009 at 2:01 PM, Robert Kern <firstname.lastname@example.org> wrote:
> There is a neat trick for accessing the diagonal of an existing array
> (a.flat[::a.shape+1]), but it won't work to implement
Perfect. That's 3x faster.
def fill_diag(arr, value):
if arr.ndim != 2:
raise ValueError, "Input must be 2-d."
if arr.shape != arr.shape:
raise ValueError, 'Input must be square.'
arr.flat[::arr.shape+1] = value
More information about the Numpy-discussion