[Numpy-discussion] Combining arrays together

Stefan van der Walt stefan at sun.ac.za
Mon Jul 3 07:48:04 EDT 2006


On Mon, Jul 03, 2006 at 11:16:26AM +0900, Bill Baxter wrote:
> What's the best way to combine say several 2-d arrays together into a grid?
> Here's the best I can see:
> 
> >>> a = eye(2,2)
> >>> b = 2*a
> >>> c = 3*a
> >>> d = 4*a
> >>> r_[c_[a,b],c_[c,d]]
> array([[1, 0, 2, 0],
>        [0, 1, 0, 2],
>        [3, 0, 4, 0],
>        [0, 3, 0, 4]])
> 
> In matlab you'd get the same effect by saying:   [ a, b; c, d ]
> 
> Compared to that,  r_[c_[a,b],c_[c,d]] looks quite a mess.

You could always explicitly write out what you are doing, i.e.

In [47]: N.vstack((N.hstack((a,b)), N.hstack((c,d))))
Out[47]:
array([[ 1.,  0.,  2.,  0.],
       [ 0.,  1.,  0.,  2.],
       [ 3.,  0.,  4.,  0.],
       [ 0.,  3.,  0.,  4.]])

Stéfan




More information about the NumPy-Discussion mailing list