1

I'm making a program where I need to make a matrix looking like this:

A = np.array([[ 1.,  2.,  3.],
   [ 1.,  2.,  3.],
   [ 1.,  2.,  3.],
   [ 1.,  2.,  3.]])

So I started thinking about this np.arange(1,4)

But, how to append n columns of np.arange(1,4) to A?

Juan Valladolid
  • 133
  • 2
  • 11

4 Answers4

3

As mentioned in docs you can use concatenate

>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6]])
>>> np.concatenate((a, b), axis=0)
array([[1, 2],
       [3, 4],
       [5, 6]])
>>> np.concatenate((a, b.T), axis=1)
array([[1, 2, 5],
       [3, 4, 6]])
Saeid
  • 4,147
  • 7
  • 27
  • 43
3

Here's another way, using broadcasting:

In [69]: np.arange(1,4)*np.ones((4,1))
Out[69]: 
array([[ 1.,  2.,  3.],
       [ 1.,  2.,  3.],
       [ 1.,  2.,  3.],
       [ 1.,  2.,  3.]])
hpaulj
  • 221,503
  • 14
  • 230
  • 353
1

You can get something like what you typed in your question with:

N = 3
A = np.tile(np.arange(1, N+1), (N, 1))

I'm assuming you want a square array?

Frank M
  • 1,550
  • 15
  • 15
0
>>> np.repeat([np.arange(1, 4)], 4, 0)
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])
John La Rooy
  • 295,403
  • 53
  • 369
  • 502