[Numpy-discussion] Combining arrays together

Bill Baxter wbaxter at gmail.com
Sun Jul 2 22:16:26 EDT 2006


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.

Would be nice if there were some operator like c_ that took a special
argument that introduced a new row.  Like maybe:

c_[a,b, newrow, c,d]

or maybe you could abuse the syntax and make something like this work:

c_[a,b : c,d]


or perhaps an empty argument could work?

c_[a,b ,, c,d]

Or empty tuple:

c_[a,b, (), c,d]

Hmm... I see there's already something in the code for handling 'special
directives':

>>> c_[a,b,'r',c,d]
matrix([[1, 0, 2, 0, 3, 0, 4, 0],
       [0, 1, 0, 2, 0, 3, 0, 4]])

'r' seems to turn the results into a matrix.  Maybe that could be used to
recognize a newline or something:

>>> c_[a,b,'\n',c,d]
Traceback (most recent call last):
  File "<input>", line 1, in ?
  File "C:\Python24\Lib\site-packages\numpy\lib\index_tricks.py", line 239,
in __getitem__
    raise ValueError, "Unknown special directive."
ValueError: Unknown special directive.

--Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20060703/e0237f6a/attachment.html>


More information about the NumPy-Discussion mailing list