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

Michael McNeil Forbes mforbes@physics.ubc...
Sat Apr 28 03:20:26 CDT 2007


How about this:

def extend(a,extent):
     """Return an extended version of a.

     The new array will have shape = (...,extent) and will be
     padded with the last 'column' of a.

     >>> a = asarray([[1,2,3],[4,5,6]])
     >>> extend(a,6)
     array([[1, 2, 3, 3, 3, 3],
            [4, 5, 6, 6, 6, 6]])
     """
     new_shape = list(a.shape)               # Make new_shape a  
mutable list
     old_extent = min(extent,new_shape[-1])  # Allow extent to shrink  
array
     new_shape[-1] = extent

     extended_a = empty(new_shape,dtype=a.dtype)

     extended_a[...,:old_extent] = a[...,:old_extent]
     extended_a[...,old_extent:] = a[...,-1:]
     return extended_a

Michael.

On 28 Apr 2007, at 12:46 AM, Stef Mientki wrote:

> hello,
>
> I've found a working solution for the problem,
> but I'm not happy with it,
> because I can't manage to do it without 2 transposes,
> and I've the feeling my solution is much too complicated.
> So I'ld be much obliged if someone gives me a better solution,
> or tell me that this is thé solution.
>
> thanks,
> Stef Mientki
>
> # start with a 2-dimensional array
> # we want to expand the second index with the last value
> a=asarray([[1,2,3],[4,5,6]])
>
> # get the last row
> b=asarray(a[:,-1])
>
> # now extend the row with 3 samples
> a=a.transpose()
> a=vstack((a,b))
> a=vstack((a,b))
> a=vstack((a,b))
> a=a.transpose()
>
> And here is the result
> [[1 2 3 3 3 3]
>  [4 5 6 6 6 6]]
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user


More information about the SciPy-user mailing list