1

I'm operating on a numpy 2-d array and trying to find some way to access different slices per row. Preferably exploiting numpy broadcasting in such a way that I pass an array of slices like follows:

A = np.array([[1,2,3,4,5],[1,4,9,16,25],[1,8,27,64,125]])
A[[2:,3:,4:]]
>>> array([[3,4,5],[16,25],[125]])

I know what I wrote above is bogus, but you get the point. I'd like to pass in an array t = [2,3,4] so that each member of that array (denote by t_i) results in t_i: (that member and a colon after).

Thanks

mbauman
  • 30,958
  • 4
  • 88
  • 123
Matthew K
  • 189
  • 2
  • 12

1 Answers1

1

What you want is probably not possible with broadcasting. But a list comprehension may work:

np.array([a[i+2:] for i,a in enumerate(A)])
#array([array([3, 4, 5]), array([16, 25]), array([125])], dtype=object)
DYZ
  • 55,249
  • 10
  • 64
  • 93