From 64a81ecfcde18e2a450b3b152a3714a84069f218 Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Sat, 30 Dec 2023 19:21:21 +0100 Subject: [PATCH] Update README.md Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com> --- numpy/ravel_unravel/README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/numpy/ravel_unravel/README.md b/numpy/ravel_unravel/README.md index c08bf98..cbee57e 100644 --- a/numpy/ravel_unravel/README.md +++ b/numpy/ravel_unravel/README.md @@ -18,6 +18,35 @@ numpy.ravel_multi_index(multi_index, dims, mode='raise', order='C') > Converts a tuple of index arrays into an array of flat indices, applying boundary modes to the multi-index. +```python +import numpy as np + +idx = np.ravel_multi_index(([0, 1, 2], [0, 1, 1]), dims=(3, 2)) +print(idx) # -> [0 3 5] + +a = np.zeros((3, 2)) +a.flat[idx] = 1 +print(a) +print() + +a = np.zeros((3, 2)) +a.flat[idx] = np.arange(1, idx.shape[0] + 1) +print(a) +``` + +Output: + +```python +[[1. 0.] + [0. 1.] + [0. 1.]] + +[[1. 0.] + [0. 2.] + [0. 3.]] +``` + + ## [numpy.unravel_index](https://numpy.org/doc/stable/reference/generated/numpy.unravel_index.html) ```python