Compoot
Compoot

Reputation: 2375

numpy 2d array using a for loop to populate it with ascending sequence of numbers

There are similar questions, but they are a) not for python or b) not using my specific structure using a for loop with a 2d numpy array in this manner.

I want to populate the following numpy array with numbers 1,2,3,4,5 etc.

At the moment it populates each row from 1 - 10, then starts again:

import numpy

a = numpy.zeros((16,11))

for i in range(11):
  for j in range(16):
    for k in range(16):
      a[j,i]=i+1



print(a)

I would like it to produce:

[[  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.]
 [ 12.  13.  14.  15.  16.  17.  18.  19.  20.  21.  22.]
 [ 23.  24.  25.  26.  27.  28.  29.  30.  31.  32.  33.]
 [ 34.  35.  36.  37.  38.  39.  40.  41.  42.  43.  44.]
 [ 45.  46.  47.  48.  49.  50.  51.  52.  53.  54.  55.]
 [ 56.  57.  58.  59.  60.  61.  62.  63.  64.  65.  66.]
 [ 67.  68.  69.  70.  71.  72.  73.  74.  75.  76.  77.]
etc for a 16 x 11 (16 rows and 11 column array)

Could someone point out the error with a clear explanation and also perhaps shed any light on an easier method to do this? (or alternative methods). I realise I may not need the third loop, but am struggling to figure out how to add the offset of 11.

Doing this in a much less efficient way ....I found that this works:

for i in range(11):
    a[0,i]=i+1
    a[1,i]=i+12
    a[2,i]=i+23
    a[3,i]=i+34
    a[4,i]=i+45
    a[5,i]=i+56
    a[6,i]=i+67

print(a)

It is the bit that adds the offset of 11, that I cannot figure out how to add to my existing structure.

Thank you in advance.

Upvotes: 0

Views: 960

Answers (1)

hpaulj
hpaulj

Reputation: 231540

Here are several ways:

The one I use all the time to generate sample arrays:

In [99]: np.arange(1,34).reshape(3,11)
Out[99]: 
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
       [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]])

It is also useful to think in terms of adding row and column coodinates. Numpy broadcasting makes this easy:

In [100]: np.arange(0,33,11)[:,None]+np.arange(1,12)
Out[100]: 
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
       [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]])

The equivalent list code:

In [101]: [[i+j for j in range(1,12)] for i in range(0,33,11)]
Out[101]: 
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
 [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
 [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]]

Something closer to your attempt; the i,j loops are fine, but you need to step the k correctly:

In [106]: k=1
In [107]: out=np.zeros((3,11),int)
In [108]: for i in range(3):
     ...:     for j in range(11):
     ...:         out[i,j] = k
     ...:         k += 1
     ...:         
In [109]: out
Out[109]: 
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
       [23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]])

Upvotes: 1

Related Questions