reindeer - 11 months ago 54

Python Question

Given a numpy 2D array of points, aka 3D array with size of the 3rd dimension equals to 2, how do I get the minimum x and y coordinate over all points?

**Examples:**

**First:**

*I edited my original example, since it was wrong.*

`data = np.array(`

[[[ 0, 1],

[ 2, 3],

[ 4, 5]],

[[11, 12],

[13, 14],

[15, 16]]])

minx = 0 # data[0][0][0]

miny = 1 # data[0][0][1]

`array([[[ 0, 77],`

[29, 12],

[28, 71],

[46, 17]],

[[45, 76],

[33, 82],

[14, 17],

[ 3, 18]],

[[99, 40],

[96, 3],

[74, 60],

[ 4, 57]],

[[67, 57],

[23, 81],

[12, 12],

[45, 98]]])

minx = 0 # data[0][0][0]

miny = 3 # data[2][1][1]

Is there an easy way to get now the minimum x and y coordinates of all points of the data? I played around with amin and different axis values, but nothing worked.

My array stores positions from different robots over time. First dimension is time, second is the index of an robot. The third dimension is then either x or y of a robots for a given time.

Since I want to draw their paths to pixels, I need to normalize my data, so that the points are as close as possible to the origin without getting negative. I thought that subtracting [minx,miny] from every point will do that for me.

Answer

Seems you need consecutive min alongaxis. For your first example:

```
>>> np.min(np.min(data, axis=1), axis=0)
array([ 0, 1])
```

For the second:

```
>>> np.min(np.min(data, axis=1), axis=0)
array([0, 3])
```

The same expression can be stated (in numpy older than 1.7), as pointed out by @Jamie, s

```
>>> np.min(data, axis=(1, 0))
array([0, 3])
```

Source (Stackoverflow)