-1

I have a python array such as:

[[1],
[2],
[3],
[4]
]

I want to make it to:
[ [1 0 0 0],
  [2 0 0 0 ],
  [3 0 0 0],
  [4 0 0 0]
]

What is the python way to do this? Suppose I use numpy.

Mazdak
  • 105,000
  • 18
  • 159
  • 188
BufBills
  • 8,005
  • 12
  • 48
  • 90
  • 1
    possible duplicate of [Good ways to "expand" a numpy ndarray?](http://stackoverflow.com/questions/12668027/good-ways-to-expand-a-numpy-ndarray) – Xiaotian Pei Jun 14 '15 at 17:04

2 Answers2

1

numpy.pad(array, pad_width, mode=None, **kwargs):

>>> np.pad(a, pad_width=((0, 0), (0, 3)), mode='constant', constant_values=0)
array([[1, 0, 0, 0],
       [2, 0, 0, 0],
       [3, 0, 0, 0],
       [4, 0, 0, 0]])
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
0

Create a row vector using numpy.eye.

>>> import numpy as np
>>> a = np.array([[1],[2],[3],[4]])
>>> b = np.eye(1, 4)
>>> b
array([[ 1.,  0.,  0.,  0.]]
>>> c = a * b
>>> c
array([[ 1.,  0.,  0.,  0.],
       [ 2.,  0.,  0.,  0.],
       [ 3.,  0.,  0.,  0.],
       [ 4.,  0.,  0.,  0.]])

Concatenation of zeros might be faster since this example uses matrix multiplication to achieve the required result and not allocation of the required size.