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

Johan Grönqvist johan.gronqvist@gmail....
Mon Sep 14 06:06:32 CDT 2009


Ruben Salvador skrev:
> [...] I want to create a 2D array where each 
> *row* is a copy of an already existing 1D array. For example:

> In [25]: a
> Out[25]: array([1, 2, 3])
> [...]
> In [30]: b
> Out[30]:
> array([[1, 2, 3],
> [1, 2, 3],
> [1, 2, 3],
> [1, 2, 3],
> [1, 2, 3]])
> 

Without understanding anything, this seems to work:

johan@johan-laptop:~$ python
Python 2.5.4 (r254:67916, Feb 18 2009, 03:00:47)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import numpy as np
 >>> a = np.array([1, 2, 3])
 >>> b = np.zeros((5, 3))
 >>> b[:, :] = a
 >>> b
array([[ 1.,  2.,  3.],
        [ 1.,  2.,  3.],
        [ 1.,  2.,  3.],
        [ 1.,  2.,  3.],
        [ 1.,  2.,  3.]])





Hope it helps

/ johan



More information about the NumPy-Discussion mailing list