diff --git a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md index 101ed3d2..9a42f3a7 100644 --- a/Python/Module3_IntroducingNumpy/AdvancedIndexing.md +++ b/Python/Module3_IntroducingNumpy/AdvancedIndexing.md @@ -695,6 +695,13 @@ This will return a tuple of two integer-valued index-arrays. These contain the i ... [ 0.84, 0.76, 0.25, 0.07]]) >>> x[np.arange(4), np.arange(4)] = range(4) +# equivalent (works for the general case of a square matrix of N-dims) +# x[tuple(np.arange(x) for x in x.shape)] = range(x.shape[0]) + +# equivalent (using numpy built-in functions): +# x[np.diag_indices_from(x)] = np.arange(4) +# np.fill_diagonal(x, np.arange(4)) + >>> x[0.8 < x] += 1 >>> x array([[ 0. , 0.05, 1.84, 0.21],