I am trying to build a function that counts up zeros in a list until nonzero entry appears, and start counting from 0 again. For example,
>>> a
array([[ 0, 0, 1, 0, 2],
[ 0, 0, 0, 1, 1],
[ 0, 1, 0, 0, 0],
[ 0, 0, 10, 2, 2],
[ 2, 0, 0, 0, 0]])
In this case, my desired output will be
array([[1, 1, 0, 1, 0],
[2, 2, 1, 0, 0],
[3, 0, 2, 1, 1],
[4, 1, 0, 0, 0],
[0, 2, 1, 1, 1]])
I tried making this with two for-loops, but it is very slow on a very large data set. I was hoping I could find a way to vectorize this operation so that the time is O(n) instead of O(n^2). Any help will be appreciated!
for i in range(n): for j in range(m):then it is O(n^2), isn't it?nis. Typically, we speak in terms of the total size of the array. So, in that case, no it is O(n). ifnis the length of the first dimension then yes, that algorithm would be O(n^2), but typically, you are interested in the big-oh complexity in terms of the total size of the array.