A faster way to evaluate 2D-array items than nested for loops?
Question: I'm using python 2.7

I have a 2-dimensional array that is several hundred elements on each axis.
Each element is either '0', or '255' I need to look at each element and write
a '0' or '1' into a different array that has the same dimensions. Currently I
have a nested for-loop structure iterating over the rows and columns.

This is turning out to be really, really slow. What is a better
implementation? What if I multiplied the matrix by a constant 1/255. This
would keep the zeros as zeros, and convert the 255 to '1'. but this is just
trading the loops for an enormous number of multiplies, which probably has its
own speed issues.

Thoughts?


Answer: I'd also use numpy.array for fast array manipulation, here's an example :

    
    
    import numpy as np
    # Create an example array of shape (100, 100) filled with either 0 or 255
    a = 255*np.random.randint(2, size=10000).reshape(100,100)
    
    # Transform it as you want
    1*(a==255)
    

