[SciPy-user] how to make vectors and arrays of the same length ?

Stef Mientki s.mientki@ru...
Fri Apr 27 11:39:34 CDT 2007


I've vectors (1d-arrays) and 2d-arrays,
which should have equal length along the last axis,
by extending the too short vectors with the value of the last sample.

And as I try to write this code (see my try below),
I've the feeling, that I'm complicating things unnecessary.

Maybe someone has a nice trick ?

Stef Mientki

b1 = floor(10*random.random((2,10)))
b2 = ones(20)

# test dimension along the last axis (= time-axis)
_ml = max ( b1.shape[-1], b2.shape[-1] )

if b1.ndim == 2 :
   if b1.shape[0]<_ml:
             last_col = b1[:,-1]
       #while b1.shape[0] < _ml:
       #b1 = vstack((b1,last_col))
       print last_col
       xc = vstack((last_col,last_col))
       print 'xc',xc
       b1 = hstack((b1,xc))
       print b1
       print xc[0]

# the 1-d case is simple (but probably can also be improved ;-)
   if len(b1)<_ml: b1 = r_ [b1, b1[-1]*ones(_ml-len(b1))]
if len(b2)<_ml: b2 = r_ [b2, b2[-1]*ones(_ml-len(b2))]

More information about the SciPy-user mailing list