2

I want to build two 2d arrays

a = [[0, 0, 0, 0, 0, 0],
     [1, 1, 1, 1, 1, 1],
     [2, 2, 2, 2, 2, 2],
     [3, 3, 3, 3, 3, 3],
     [4, 4, 4, 4, 4, 4],
     [5, 5, 5, 5, 5, 5]]

b = [[0, 1, 2, 3, 4, 5],
     [0, 1, 2, 3, 4, 5],
     [0, 1, 2, 3, 4, 5],
     [0, 1, 2, 3, 4, 5],
     [0, 1, 2, 3, 4, 5],
     [0, 1, 2, 3, 4, 5]]

But i cannot use for loops. i know i can get an array using np.arange(5), but not sure how to turn that into the 2 2D arrays shown above. Any help would be greatly appreciated

Coder
  • 151
  • 2
  • 12

4 Answers4

5

You can use numpy.mgrid or numpy.meshgrid():

np.mgrid[0:6, 0:6]
# array([[[0, 0, 0, 0, 0, 0],
#         [1, 1, 1, 1, 1, 1],
#         [2, 2, 2, 2, 2, 2],
#         [3, 3, 3, 3, 3, 3],
#         [4, 4, 4, 4, 4, 4],
#         [5, 5, 5, 5, 5, 5]],
# 
#        [[0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5]]])

np.meshgrid(np.arange(6), np.arange(6))
# [array([[0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5],
#         [0, 1, 2, 3, 4, 5]]),
#  array([[0, 0, 0, 0, 0, 0],
#         [1, 1, 1, 1, 1, 1],
#         [2, 2, 2, 2, 2, 2],
#         [3, 3, 3, 3, 3, 3],
#         [4, 4, 4, 4, 4, 4],
#         [5, 5, 5, 5, 5, 5]])]

and simply unpack the result

a, b = np.mgrid[0:6, 0:6]    
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
4

You just need numpy.reshape and numpy.repeat. Use this:

import numpy as np

n_columns = 6
a = np.repeat(np.arange(6), n_columns) 
a = a.reshape(6,n_columns)

array([[0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5]])

b = a.T

array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

This code will work for any n_columns value.

seralouk
  • 30,938
  • 9
  • 118
  • 133
2

There is also np.indices:

 I,J = np.indices((6,6))
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
1

You can try np.repeat:

>>> x = np.arange(6).reshape(1,-1)
>>> y = np.repeat(x,6,axis=0)
>>> y
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])
>>> z = y.T
>>> z
array([[0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 0],
       [2, 2, 2, 2, 2, 0],
       [3, 3, 3, 3, 3, 0],
       [4, 4, 4, 4, 4, 0],
       [5, 5, 5, 5, 5, 0]])
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • What's with the downvote? Can anybody explain? – Sayandip Dutta Nov 15 '19 at 08:20
  • The dimensions are not the same as the OP. Also this looks identical to the above answer (before my edits) and posted 2 min later. Cheers – seralouk Nov 15 '19 at 08:28
  • Misunderstanding -- I did not (myself) downvote. I just said why someone could downvote it. – seralouk Nov 15 '19 at 08:43
  • And just for the record, initially my 3 lines would address exactly the OP. then I edited to post the results/outputs – seralouk Nov 15 '19 at 08:46
  • It was not, your first post was: `import numpy as np; a = np.repeat(np.arange(6),5); a.reshape(6,5)`. So you did not address the `.T` aspect in the first go. However, I am not saying you copied mine, or you did not know the transpose. A misunderstanding it is. Regardless, your first attempt was an incomplete one by long stretch, so try not be [FGITW](https://meta.stackexchange.com/questions/18014/what-is-fgitw-and-scite) unless you address all the issues, you can beautify with edits later, that's fine. – Sayandip Dutta Nov 15 '19 at 08:52