MATLAB 2D索引A([],[])的Numpy等效命令
我正在寻找Numpy中的精确命令以遵循MATLAB索引。
上传为图片: [1]: https://i.sstatic.net/q2dj0.png
我已经尝试过在numpy中做类似的事情:
kk = np.zeros((100,100))
k= np.array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]])
kk[[9,2,7],[9,2,7]] = k
但是这会给您带来错误:
ValueError: shape mismatch: value array of shape (3,3) could not be broadcast to indexing result of shape (3,)
我编辑了这个问题,就我而言,每个索引都不是连续的,但是它们是相同的:kk [[9,2,7],[9,[9,[9, 2,7]]。
I am looking for exact command in Numpy for following Matlab indexing.
Uploaded as picture:
[1]: https://i.sstatic.net/Q2DJ0.png
I have tried to do similar thing in Numpy:
kk = np.zeros((100,100))
k= np.array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]])
kk[[9,2,7],[9,2,7]] = k
But this will throw you error:
ValueError: shape mismatch: value array of shape (3,3) could not be broadcast to indexing result of shape (3,)
i edit this question, in my case, each indexing is not contiguous, but they are the same for example: kk[[9,2,7],[9,2,7]].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果索引是连续的,则应使用
slice()
s:请注意,
a:b:b:b:c
内部[]
是切片(a,b,c),带有
:c
/,c
可选,如果a
/b
应该是none
可以在快捷方式中遗漏(但在功能版本中不要),并且如果仅将一个参数设置为slice()
,则此分配给b
。否则,您可以使用
numpy.ix _()
:
切片通常比高级索引更快,更有效的内存效率,您应该在可能的情况下更喜欢它们。
请注意,
np.ix _()
只是生成具有正确形状的索引数组,以触发所需的索引:因此,以下是有效的:
此外,
slice> slice
s andnp.ndarray(dtype = int)
可以合并在一起:If the indexing is contiguous you should use
slice()
s:Note that
a:b:c
inside[]
is sugar syntax forslice(a, b, c)
, with:c
/, c
optional and ifa
/b
should beNone
this can be left out in the shortcut (but not in the functional version) and if only one parameter is set toslice()
, this is assigned tob
.Otherwise, you could use
numpy.ix_()
:Slices are typically way faster and more memory efficient than advanced indexing, and you should prefer them when possible.
Note that
np.ix_()
is just producing index arrays with the correct shapes to trigger the desired indexing:Hence, the following would work:
Also,
slice
s andnp.ndarray(dtype=int)
can be combined together: