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
  • 113
  • 2
  • 9

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,079
  • 7
  • 26
  • 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
  • 201,845
  • 13
  • 203
  • 313
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,470
  • 15
  • 14
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
  • 281,034
  • 50
  • 354
  • 495