[Numpy-discussion] Create 2D array from EXISTING 1D array

Ruben Salvador rsalvador.wk at gmail.com
Mon Sep 14 06:46:50 EDT 2009


Hi there!

It's a time since I'm asking this question to myself, and still don't know a
Pythonic way to solve it. I want to create a 2D array where each *row* is a
copy of an already existing 1D array. For example:

In [21]: a = np.array[1, 2, 3]
In [25]: a
Out[25]: array([1, 2, 3])

To create a 2D 'b' array, where each row is 'a', I would do:

In [28]: b = np.empty((5,3), np.int)

In [29]: for i in range(5):
....: b[i] = a
....:
....:

In [30]: b
Out[30]:
array([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])

I can't figure out how to create the same 2D array with np.array(), or
np.whatever(). It should be faster, cleaner...

Any idea?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090914/6ae74e97/attachment.html>


More information about the NumPy-Discussion mailing list