i-am-spartacus - 1 year ago 71

Python Question

I have a 3D matrix, within which I have identified a "region of interest" - a bounding box - which I would like the indices for.

I have the upper and lower bound limits of each axis:

`([x:13 y:5 z:21] [x:27 y:29 z:35])`

But I am not quite sure how to get all of the indices for values inside this box. I believe the "order" I have currently using for these is C conventional which I should keep.

Is there a simple way to do this using python?

Thanks!

Answer

Use `numpy.where`

to find the indices of an array fulfilling a condition. `numpy.where`

returns a tuple, the first element contains the indices.

```
from __future__ import print_function
import numpy
a = numpy.arange(0, 100, 10) # 0, 10, 20, ...
ind = numpy.where(a>30)[0]
print(ind) # [4 5 6 7 8 9]
```

Source (Stackoverflow)