I'm trying to find the fastest way to find the first non-zero value for each row of a two dimensional sorted array. Technically, the only values in the array are zeros and ones, and it is "sorted".

For instance, the array could look like the following:

v =

```
0 0 0 1 1 1 1
0 0 0 1 1 1 1
0 0 0 0 1 1 1
0 0 0 0 0 0 1
0 0 0 0 0 0 1
0 0 0 0 0 0 1
0 0 0 0 0 0 0
```

I could use the argmax function

```
argmax(v, axis=1))
```

to find when it changes from zero to one, but I believe this would do an exhaustive search along each row. My array will be reasonably sized (~2000x2000). Would argmax still outperform just doing a searchsorted approach for each row within a for loop, or is there a better alternative?

Also, the array will always be such that the first position of a one for a row is always >= the first position of a one in the row above it (but it is not guaranteed that there will be a one in the last few rows). I could exploit this with a for loop and a "starting index value" for each row equal to the position of the first 1 from the previous row, but am i correct in thinking that the numpy argmax function will still outperform a loop written in python.

I would just benchmark the alternatives, but the edge length of the array could change quite a bit (from 250 to 10,000).

argmax() use C level loop, it's much faster than Python loop, so I think even you write a smart algorithm in Python, it's hard to beat argmax(), You can use Cython to speedup:

```
@cython.boundscheck(False)
@cython.wraparound(False)
def find(int[:,:] a):
cdef int h = a.shape[0]
cdef int w = a.shape[1]
cdef int i, j
cdef int idx = 0
cdef list r = []
for i in range(h):
for j in range(idx, w):
if a[i, j] == 1:
idx = j
r.append(idx)
break
else:
r.append(-1)
return r
```

On my PC for 2000x2000 matrix, it's 100us vs 3ms.

It is reasonably fast to use np.where:

```
>>> a
array([[0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 0]])
>>> np.where(a>0)
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 3, 4, 5]), array([3, 4, 5, 6, 3, 4, 5, 6, 4, 5, 6, 6, 6, 6]))
```

That delivers tuples with to coordinates of the values greater than 0.

You can also use np.where to test each sub array:

```
def first_true1(a):
""" return a dict of row: index with value in row > 0 """
di={}
for i in range(len(a)):
idx=np.where(a[i]>0)
try:
di[i]=idx[0][0]
except IndexError:
di[i]=None
return di
```

Prints:

```
{0: 3, 1: 3, 2: 4, 3: 6, 4: 6, 5: 6, 6: None}
```

ie, row 0: index 3>0; row 4: index 4>0; row 6: no index greater than 0

As you suspect, argmax may be faster:

```
def first_true2():
di={}
for i in range(len(a)):
idx=np.argmax(a[i])
if idx>0:
di[i]=idx
else:
di[i]=None
return di
# same dict is returned...
```

If you can deal with the logic of not having a `None`

for rows of all naughts, this is faster still:

```
def first_true3():
di={}
for i, j in zip(*np.where(a>0)):
if i in di:
continue
else:
di[i]=j
return di
```

And here is a version that uses axis in argmax (as suggested in your comments):

```
def first_true4():
di={}
for i, ele in enumerate(np.argmax(a,axis=1)):
if ele==0 and a[i][0]==0:
di[i]=None
else:
di[i]=ele
return di
```

For speed comparisons (on your example array), I get this:

```
rate/sec usec/pass first_true1 first_true2 first_true3 first_true4
first_true1 23,818 41.986 -- -34.5% -63.1% -70.0%
first_true2 36,377 27.490 52.7% -- -43.6% -54.1%
first_true3 64,528 15.497 170.9% 77.4% -- -18.6%
first_true4 79,287 12.612 232.9% 118.0% 22.9% --
```

If I scale that to a 2000 X 2000 np array, here is what I get:

```
rate/sec usec/pass first_true3 first_true1 first_true2 first_true4
first_true3 3 354380.107 -- -0.3% -74.7% -87.8%
first_true1 3 353327.084 0.3% -- -74.6% -87.7%
first_true2 11 89754.200 294.8% 293.7% -- -51.7%
first_true4 23 43306.494 718.3% 715.9% 107.3% --
```

Similar Questions

I'm making my first real foray into Python and NumPy to do some image processing. I have an image loaded as a 3 dimensional NumPy Array, where axis 0 represents image bands, while axes 1 and 2 represe

Alright, extreme rookie question here. In my program, I generate a 2D numpy array, some of whom's entries are missing (not the nan kind of nonexistant, but the None kind, or NoneType). I'd like to

I have a two dimensional array which has following structure a = [[5/2013, 20.0, 6.0, 6.0], [7/2013, 73.0, 66.0, 66.0], [50/2013, 11530.0, 12625.27, 12087.75], [2/2014, 5926.0, 6058.0, 5876.4]

K and THREADS are arbitrarily defined as follows: #define K 1000 #define THREADS 2 Let's say I'm responsible for doing some stuff with a two-dimensional array called: long A[K][K]; I need to pass th

For my homework it is given one dimensional array and i have to convert it in a two dimensional array. The two dimensional array has 2 for the number of columns, because i have to represent the one di

Is there a way to return the indices of k-minimum values along an axis of a numpy array without using loops?

I'm currently working on a webshop. For that i need to make a two dimensional array to store the items moved to the cart. Cart: Cart = Session(Cart) Items = Session(Items) And when an item is mov

Surely there must be a way to do this... I can't work it out. I have a (9,4) array, and I want to repeat it along a 3rd axis 4096 times... So it becomes simply (9,4,4096), with each value from the 9,4

I need to pick a value based on a specific key from an two-dimensional array, how would I do that? I only know the key of the second level in the array in my code, and not in which array key it sits i

I have a 3d Numpy array and would like to take the mean over one axis considering certain elements from the other two dimensions. This is an example code depicting my problem: import numpy as np myarr

Can anybody suggest which is the best way for using NSArray as two-dimensional array to store data for filtering data based on field. Thanks in advance Regards, sathish

I have a 1-dimensional numpy array scores some sort of scores associated with some objects. These objects belong to some disjoint groups, and all the scores of the items in the first group are first,

I have a three dimensional array, say dat.shape = (100,128,256). I'm trying to count the number of periods that have values greater than 10.0 across the first axis. For example, for dat[:,0,0], how ma

I want to filter a array by a number and update its status in the first array. I have two array $arr1,$arr2 $arr1 = array( 0=>array('number'=>100,name=>'john'), 1=>array('number'=>200,n

I asked a question a while ago and as a result of the answers I received I revised my program significantly. I am having trouble now though using the strcmp method with a two dimensional array. Here i

I wanted to know if it is possible to convert two single dimensional array into one two dimensional array using arraylists. Here's my code: String[] user = (String[])compList.toArray(new String[usersL

I had a following interview question. There is an array of nxn elements. The array is partially sorted i.e the biggest element in row i is smaller than the smallest element in row i+1. How can you fin

I want to merge two two-dimensional array in perl. For example, @array1 and @array2 are two-dimensional arrays. @array1 has 200 rows and 300 columns. @array2 has 200 rows and 100 columns. My question

I need to read values from excel worksheet into 2d array.can anyone tell me how to do this using pythonwin32com.

I'm trying to run a custom kmeans clustering algorithm and am having trouble getting the document frequency for each column(term) of a 2-d numpy array by cluster. My current algorithm has two numpy ar

This question already has an answer here: Need an algorithm for a special array (linear field) 3 answers I have tried the following code to find out the minimum element in a cyclic sorted array

Hello I am new to C++ and am having trouble understanding why this two dimensional array is only producing one row and many columns. It reads the correct information but does not output it with the co

Please consider the following 2-D Array: int array[2][2] = { {1,2}, {3,4} }; As per my understanding: - 'array' represents the base address of the 2-D array (which is the same as address of the first

Is this the least amount of code I can use to convert a Dapper query result to a two dimensional array? var array = cn.Query(@SELECT Id, Desc FROM Things) .Select<object, ArrayList>(d => {

I have seen this question, but want to reduce the array created from mask = array == value mask = array([[[ True, True, True], [False, True, True]], [[False, True, True], [False, True, True]], [[False

the array[time, value]; I need to total/sum of value from this two dimensional array? var array =[ [1361824790262, 90.48603343963623], [1361828390262, 500.18687307834625], [1361831990262, 296.05108177

I have a controller that returns two dimensional array of objects as json. I make a call from my javascript: $.getJSON(/Game/GetBoard, function (json) { data = json; board = ko.mapping.fromJS({ boar

I want to have static and constant two dimensional array inside a class. The array is relatively large, but I only want to initialize a few elements and others may be whatever compiler initializes the

status_name=Array(a,b,c,b,e,f); status_id=Array( 1, 2, 3, 4, 5, 6); How to combine these two arrays and to built multi dimensional array Expected Multidimensional array be like this [[a

Is there a numpy function to sum an array along (not over) a given axis? By along an axis, I mean something equivalent to: [x.sum() for x in arr.swapaxes(0,i)]. to sum along axis i. For example, a ca

So I have a ONE dimensional array with N values, where N is a perfect square. I visualize this one dimensional array as a two dimensional array (although it is not). For example, an array with values

Am new to Javafx and wondering how to populate a tableview from a 2-dimensional array of String: String[][] staffArray = (String[][]) connection.getAll(StaffServices); ObservableList row = FXCollec

I am trying to display a 2-dimensional array. int arr[numRows][numCols] = {{0,1,2,3,4,5,6},{7,8,9,10,11,12,13},{14,15,16,17,18,19,20}}; void display(int p[][numCols]) { //printf(\n); for (int i = 0;

say that I have a (40,20,30) numpy array and that I have a function that after some work will return half of the input array along a selected input axis. Is there an automatic way to do so ? I would l

I'm trying to create a booking service and I've been stuck on this part for many hours and I just can't figure out what I'm doing wrong. So I've got a Two Dimensional array and when trying to print o

I would like to be able to shift a 3D numpy array in either direction along the height axis. If a positive shift of a say 3 is given, all values in the array shift up and 3 new slices of zeros appear

Is there a straight-forward way to use the output of calling NumPy's argmax or argmin functions on a single dimension of an N-D array to define an index into that array? This is probably best explaine

I have a numpy array (sums_norm) of dimension (44,36) and I would like to fit all its rows with a known function. (This question is an extension of the already asked curve fitting with a known functio

Would like to build a list of indices into a 2 dimensional bool_ array, where True. import numpy arr = numpy.zeros((6,6), numpy.bool_) arr[2,3] = True arr[5,1] = True results1 = [[(x,y) for (y,cell) i

Sorry I really cant find the error in my code. So i had this code which populates a 2 dimensional array with certain random values which act like a board and has a current size of 5x5. function creat

for(int i = 0; i<100; i++) for(int j = 0; j<100; j++) array[j][i] = 0; // array[i][j] = 0; My professor said it was much more costly to initialize a two dimensional array in the first way as op

Ok, say I have an array like so [[z,1], [d,3], [e,2]], how can I sort this array by the second element of each constituent array? So that my array would look like the following? [[z,1], [e,2], [d,3]]?

I am trying to print out two dimensional arrays from the results of an sql statement in php so far i have this code for ($i=0; $i < count($searchResults); $i++) { print <tr>; print <td&

How to memcpy the two dimensional array in C: I have a two dimensional array: int a[100][100]; int c[10][10]; I want to use memcpy to copy the all the values in array c to array a, how to do this usi

Problem is, my JSON string looks like this: jsonString = [[1,100],[2,200],[3,300]] I need to make a two dimensional array out of it in Java. If I write JSONObject jObs = new JSONObject(

I've got a 900 x 650 2D numpy array which I'd like to split into 10 x 10 blocks, which will be checked for nonzero elements. Is there a Pythonic way that I can achieve this with numpy? I'm looking for

I have a class that needs to have a two dimensional array that I can initialize during construction by passing it two parameters. How do you declare a two-dimensional dynamic array within a class. cla

I know that a single staric pointer like *p can be used as a one dimensional array. But how can I use a double staric pointer like **p as two dimensional array?

The question in simple. What i have and what's the problem? I do have two dimensional array $someArray[][]. The first bracket i could put subject or date. The second one, goes from 1 to 4 (just an

I need to slice a moderately sized 2d Numpy array along two dimensions. As example, import numpy as np X = np.random.normal(loc=0, scale=1, size=(3000, 100)) From this array, I need to select a large