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.
np.arange(1, 11*16 + 1).reshape(16, 11). If you want to keep your structure: thekloop is indeed unnecessary. just change the assignment toa[j, i] = 11*j + i + 1np.arange(1,n)followed by areshape? I do that all the time to construct a sample matrix.