1

I'm currently padding a multi-dimensional array (to allow for transposition among other things), and while I've found a range of ways of doing this with loops (np.pad and row by row assignment to a size controlled empty array) I can't seem to vectorized this operation.

MWE

a = np.arange(1,3)
b = np.arange(3,7)
c = np.arange(7,10)
source = np.array([a,b,c])

This has the form:

>>> array([array([1, 2]), array([3, 4, 5, 6]), array([7, 8, 9])], dtype=object)

My desired output is:

>>> array([[ 1.,  2.,  1.,  1.],
           [ 3.,  4.,  5.,  6.],
           [ 7.,  8.,  9.,  1.]])

In this trivial case I used:

desired = np.array([np.concatenate([a,np.ones(2)]),b,np.concatenate([c,np.ones(1)])])

(my belief is that the above wouldn't scale well as you would have to store the length of each array - which is also what stopped me using np.pad)

draco_alpine
  • 769
  • 11
  • 25

0 Answers0