[Numpy-discussion] Functions for indexing into certain parts of an array (2d)
kwgoodman at gmail.com
Sat Jun 6 18:02:44 EDT 2009
On Sat, Jun 6, 2009 at 2:01 PM, Robert Kern <robert.kern at gmail.com> 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