I'm looking for sort of the opposite of sum()
I guess. Here we go:
x = array([
[False, False, False, False, False],
[ True, False, False, False, False],
[ True, True, False, False, False],
[ True, True, True, False, False]])
x.sum(axis=1)
Out: array([0, 1, 2, 3])
So I want to go the opposite direction: from [0,1,2,3]
to an array like x
(I can specify the number of columns I want in x, of course, above it's 5).
The solution should ideally work for higher dimensions too, and I don't want to loop in Python of course, because the input could be longer than this example. That said, here's a solution using a loop:
s = np.array([0, 1, 2, 3])
y = np.zeros((len(s), 5), np.bool)
for row,col in enumerate(s):
y[row,0:col] = True
x
, and you createy
, but earlier you say to want to go from [0, 1, 2, 3] tox
(i.e.x
is the output). Isn't the operation from [0,1,2,3] to the bigger boolean array what you want? – Warren Weckesser Oct 11 '14 at 4:12arange(A.max()+1)
to<
(as in @DSM's answer). – Warren Weckesser Oct 11 '14 at 4:15