A matrix problem. Please help!

Jeff Epler jepler at unpythonic.net
Wed Feb 4 16:20:40 EST 2004


Well, my strategy is to get a new array with 1 in the spots where you
want a 1 in the output, and other values elsewhere.  Then I can use
comparison to get a (boolean) array with 1s in the right place

I didn't find a satisfactory way to do that, but here's one that seemed
to work:
>>> ru
array([[0, 0, 1, 0, 1],
       [1, 0, 0, 1, 1],
       [0, 0, 0, 1, 1],
       [1, 1, 1, 0, 0],
       [1, 1, 0, 1, 0]])
>>> N.add.accumulate(N.add.accumulate(ru, 1), 1)
array([[ 0,  0,  1,  2,  4],
       [ 1,  2,  3,  5,  8],
       [ 0,  0,  0,  1,  3],
       [ 1,  3,  6,  9, 12],
       [ 1,  3,  5,  8, 11]])
>>> _ == 1
array([[0, 0, 1, 0, 0],
       [1, 0, 0, 0, 0],
       [0, 0, 0, 1, 0],
       [1, 0, 0, 0, 0],
       [1, 0, 0, 0, 0]], type=Bool)

The first accumulate makes everything to the left of a 1 >0, and the
second accumulate makes it >1.

Jeff




More information about the Python-list mailing list