A matrix problem. Please help!
Carl
phleum_nospam at chello.se
Thu Feb 5 02:30:53 EST 2004
Jeff Epler wrote:
> 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
Jeff,
Your solution works very well. I tried to do something similar, but with
N.cumsum instead of N.add.accumuluate. However, I never did the second
step, so I ended up with two "1:s" in each row and was, therefore, never
able to do the booelan comparison.
Thanks for yopur help!
Carl
More information about the Python-list
mailing list