Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

numpy array position of each element along one axis

I’ve got a numpy 2D array, let’s say

a = np.arange(1,7).reshape(2,3)
array([[1, 2, 3],
       [4, 5, 6]])

and I’d like to have an array with the position of each element of this array along one axis like so:

array([[0, 1, 2],
       [0, 1, 2]])

I need this for the arguments to matplotlib.pyplot‘s bar3d.
Is there a simple way to do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use np.tile:

import numpy as np

a = np.arange(1,7).reshape(2,3)

n_rows, n_cols = a.shape
res = np.tile(np.arange(n_cols), (n_rows, 1))
print(res)

Output

[[0 1 2]
 [0 1 2]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading