jeudi 29 septembre 2022

numpy - how to increment by one all values on axis 1

I would like to increment all values on axis 1 by one.

For instance, row[0] col[0] increases by 1, row[1] col[0] by 2, and so on...

Example of how to do this with a single-dimensional array:

y = np.random.random(size=(10,))
y += np.arange(start=1, stop=11, step=1)

result:

[ 1.66704813  2.90625487  3.961399    4.64969495  5.5286593   6.31736865
  7.24056032  8.25632077  9.02458071 10.1816335 ]

Now I have a multi-dimensional array I would like to do the same as above only by columns

y = np.random.random(size=(10,3))
y += np.arange(start=1, stop=11, step=1)  # Not going to work. Need each col to grow by one each row increment. like the single-dimensional array only by column.

end result is something like:

[[1.66704813 1.90625487 1.961399  ]
 [2.64969495 2.5286593  2.31736865]
 [3.24056032 3.25632077 3.02458071]
 [4.1816335  4.13044436 4.13969493]
 [5.48485214 5.58980863 5.59890548]
 [6.58126238 6.30004674 6.730429  ]
 [7.76121817 7.94366992 7.1039714 ]
 [8.81874117 8.00239219 8.16807975]
 [9.64509574 9.75334071 9.59641831]
 [10.11176155 10.71027095 10.77104173]]



Aucun commentaire:

Enregistrer un commentaire