I want to create a 3d array where basically the content is identical to the indeces used to access it. So m[2,5]
would result in array([2, 5])
. I couldn't find an obvious solution with the numpy functions indices
, ogrid
, concatenate
, etc.
At the moment I'm using this, but was wondering whether there is a solution that makes better use of the API:
a, b = 3, 4
m = np.ones((a, b, 2))
for x in range(a):
m[x,:, 1] = np.array(range(b))
for y in range(b):
m[:,y,0] = np.array(range(a))
Try np.mgrid
:
a, b = 3, 4
m = np.mgrid[:a,:b].transpose(1,2,0)
print(m[1,2])
# array([1, 2])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.