How to split an two-dimension array by the second dimension?(Python)

Viewed 61

I have an array of points, and I want to split these into two arrays by the second dimension:

points_right = points[points[:, 0] > p0[0]]
points_left = points[points[:, 0] < p0[0]]

how can I split these points in one loop?

1 Answers

I think np.split is what you're looking for, just use axis=1.

Example splitting a 2x4 matrix:

import numpy as np

pts = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
left_pts, right_pts = np.split(pts, indices_or_sections=2, axis=1)

The original matrix (pts):

[[1 2 3 4]
 [5 6 7 8]]

left_pts:

[[1 2]
 [5 6]]

right_pts:

[[3 4]
 [7 8]]

https://numpy.org/doc/stable/reference/generated/numpy.split.html

Related