MATLAB 2D索引A([],[])的Numpy等效命令

发布于 2025-02-09 12:46:17 字数 627 浏览 1 评论 0原文

我正在寻找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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

决绝 2025-02-16 12:46:17

如果索引是连续的,则应使用slice() s:

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[1:3, 1:4] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 3 0 0 0]
#  [0 4 5 6 0 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

请注意,a:b:b:b:c内部[]切片(a,b,c),带有:c/,c可选,如果a/b应该是none可以在快捷方式中遗漏(但在功能版本中不要),并且如果仅将一个参数设置为slice(),则此分配给b

否则,您可以使用 numpy.ix _()

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[np.ix_((1, 3), (1, 2, 4))] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 0 3 0 0]
#  [0 0 0 0 0 0 0]
#  [0 4 5 0 6 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

切片通常比高级索引更快,更有效的内存效率,您应该在可能的情况下更喜欢它们。

请注意,np.ix _()只是生成具有正确形状的索引数组,以触发所需的索引:

np.ix_((1, 3), (1, 2, 4))
# (array([[1],
#         [3]]), array([[1, 2, 4]]))

因此,以下是有效的:

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[np.array([1, 3])[:, None], np.array([1, 2, 4])[None, :]] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 0 3 0 0]
#  [0 0 0 0 0 0 0]
#  [0 4 5 0 6 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

此外,slice> slice s and np.ndarray(dtype = int)可以合并在一起:

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[1:4:2, np.array([1, 2, 4])] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 0 3 0 0]
#  [0 0 0 0 0 0 0]
#  [0 4 5 0 6 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

If the indexing is contiguous you should use slice()s:

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[1:3, 1:4] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 3 0 0 0]
#  [0 4 5 6 0 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

Note that a:b:c inside [] is sugar syntax for slice(a, b, c), with :c/, c optional and if a/b should be None this can be left out in the shortcut (but not in the functional version) and if only one parameter is set to slice(), this is assigned to b.

Otherwise, you could use numpy.ix_():

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[np.ix_((1, 3), (1, 2, 4))] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 0 3 0 0]
#  [0 0 0 0 0 0 0]
#  [0 4 5 0 6 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

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:

np.ix_((1, 3), (1, 2, 4))
# (array([[1],
#         [3]]), array([[1, 2, 4]]))

Hence, the following would work:

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[np.array([1, 3])[:, None], np.array([1, 2, 4])[None, :]] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 0 3 0 0]
#  [0 0 0 0 0 0 0]
#  [0 4 5 0 6 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]

Also, slices and np.ndarray(dtype=int) can be combined together:

import numpy as np


kk = np.zeros((6, 7), dtype=int)
k = np.arange(2 * 3).reshape((2, 3)) + 1
kk[1:4:2, np.array([1, 2, 4])] = k
   
print(kk)
# [[0 0 0 0 0 0 0]
#  [0 1 2 0 3 0 0]
#  [0 0 0 0 0 0 0]
#  [0 4 5 0 6 0 0]
#  [0 0 0 0 0 0 0]
#  [0 0 0 0 0 0 0]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文