[Numpy-discussion] Interleaved Arrays and
Robert
kxroberto at googlemail.com
Tue Jun 16 15:18:02 EDT 2009
Ian Mallett wrote:
>
> n = #blah
> testlist = []
> for x in xrange(n):
> for y in xrange(n):
> testlist.append([x,y])
> testlist.append([x+1,y])
>
> If "testlist" is an array (i.e., I could go: "array(testlist)"), it
> works nicely. However, my Python method is certainly improveable with
> numpy. I suspect the best way is interleaving the arrays [x,y->yn] and
> [x+1,y->yn] n times, but I couldn't figure out how to do that...
>
e.g with column_stack
>>> n = 10
>>> xx = np.ones(n)
>>> yy = np.arange(n)
>>> aa = np.column_stack((xx,yy))
>>> bb = np.column_stack((xx+1,yy))
>>> aa
array([[ 1., 0.],
[ 1., 1.],
[ 1., 2.],
[ 1., 3.],
[ 1., 4.],
[ 1., 5.],
[ 1., 6.],
[ 1., 7.],
[ 1., 8.],
[ 1., 9.]])
>>> bb
array([[ 2., 0.],
[ 2., 1.],
[ 2., 2.],
[ 2., 3.],
[ 2., 4.],
[ 2., 5.],
[ 2., 6.],
[ 2., 7.],
[ 2., 8.],
[ 2., 9.]])
>>> np.column_stack((aa,bb))
array([[ 1., 0., 2., 0.],
[ 1., 1., 2., 1.],
[ 1., 2., 2., 2.],
[ 1., 3., 2., 3.],
[ 1., 4., 2., 4.],
[ 1., 5., 2., 5.],
[ 1., 6., 2., 6.],
[ 1., 7., 2., 7.],
[ 1., 8., 2., 8.],
[ 1., 9., 2., 9.]])
>>> cc = _
>>> cc.reshape((n*2,2))
array([[ 1., 0.],
[ 2., 0.],
[ 1., 1.],
[ 2., 1.],
[ 1., 2.],
[ 2., 2.],
[ 1., 3.],
[ 2., 3.],
[ 1., 4.],
[ 2., 4.],
[ 1., 5.],
[ 2., 5.],
[ 1., 6.],
[ 2., 6.],
[ 1., 7.],
[ 2., 7.],
[ 1., 8.],
[ 2., 8.],
[ 1., 9.],
[ 2., 9.]])
>>>
However I feel too, there is a intuitive abbrev function like
'interleave' or so missing in numpy shape_base or so.
Robert
More information about the NumPy-Discussion
mailing list