简体   繁体   English

如何创建多维索引数组?

[英]How do I create a multi-dimensional index array?

I want to create a 3d array where basically the content is identical to the indeces used to access it.我想创建一个 3d 数组,其中的内容基本上与用于访问它的索引相同。 So m[2,5] would result in array([2, 5]) .所以m[2,5]会导致array([2, 5]) I couldn't find an obvious solution with the numpy functions indices , ogrid , concatenate , etc.我找不到 numpy 函数indicesogridconcatenate等的明显解决方案。

At the moment I'm using this, but was wondering whether there is a solution that makes better use of the API:目前我正在使用它,但想知道是否有更好地利用 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 :尝试np.mgrid

a, b = 3, 4
m = np.mgrid[:a,:b].transpose(1,2,0)

print(m[1,2])
# array([1, 2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM