I am trying to make a code that converts integers in array to a given base and padding them to make them from the same size. The following code which I manipulated from a code on stackoverflow by Alex Martelli, doesn't work when I apply numpy.vectorize
on it, although it works for single arrays:
def int2base(x, base,size):
ret=np.zeros(size)
if x==0: return ret
digits = []
while x:
digits.append(x % base)
x /= base
digits.reverse()
ret[size-len(digits):]=digits[:]
return ret
vec_int2base=np.vectorize(int2base)
vec_int2base(np.asarray([2,1,5]),base=3,size=3)
Which terminates with the following error:
...
1640 if ufunc.nout == 1:
1641 _res = array(outputs,
-> 1642 copy=False, subok=True, dtype=otypes[0])
1643 else:
1644 _res = tuple([array(_x, copy=False, subok=True, dtype=_t)
ValueError: setting an array element with a sequence.
Is there any better way to write it for vectors case, and what am I missing here.