0

I have an array in python made like this:

array([     18,      36,      54, ..., 9893804, 9893822, 9893840],
      dtype=int64)

I wish to obtain an array containing the "distances" bewteen ech byte...

in this case it would be: [18,18,18, ..., xxx, 18, 18]

to do that I use an ugly for i in range(len(arr))

I suspect there is a way by using np.flatnonzero(something) I could not find it.. any clue?

Zibri
  • 9,096
  • 3
  • 52
  • 44

1 Answers1

2

How about np.diff(arr)?

arr = np.array(...)
print(np.diff(arr)) # [18 18 18 ... 18 18 18]
Guy
  • 46,488
  • 10
  • 44
  • 88
  • very nice! but I have a problem: if an element is >256 it must become 4 elements so that the first is 00 and the other 3 are the number in little endian... – Zibri Sep 14 '21 at 21:31
  • so I can do: np.argwhere(data > 255) to get the indices ... obviously I could insert the required data in a for loop but I am learning numpy.. and I don't know how to do it with numpy internals – Zibri Sep 14 '21 at 21:45
  • @Zibri I'm not sure what is the issue, can you please give an example and the expected output? – Guy Sep 15 '21 at 14:27
  • @i have to create an array with the distances. like you did. but if the distance is >255 I have to write 00 XX XX XX where XXXXX is the distance in little endian. – Zibri Sep 21 '21 at 13:02
  • so if I do: `z=np.argwhere(data > 255) np.where(data<256,data,data*4096) ` I get an array with the distance*4096 but as a single element I need 4 – Zibri Sep 21 '21 at 13:04
  • what I need is this: any idea? https://stackoverflow.com/questions/69269305/how-to-do-a-rising-edge-detection-on-a-numpy-array-and-get-also-the-distances – Zibri Dec 10 '21 at 14:32