”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 如何从2D数组中提取元素?使用另一数组的索引

如何从2D数组中提取元素?使用另一数组的索引

发布于2025-04-30
浏览:735

How to Extract Elements from a 2D Array Using Indices from Another Array?

Using NumPy Array as Indices for the 2nd Dimension of Another Array

To extract specific elements from a 2D array based on indices provided by a second array, you can leverage NumPy's integer array indexing.

Consider this示例:

,[2,3],[4,5]]) b = np.Array([[1],[0],[1],[1],dtype ='int')
A = np.array([[0,1], [2,3], [4,5]])
B = np.array([[1], [0], [1]], dtype='int')
,[2],[2],[2],[2],[5],[5],[5]])方法:

),b.ravel()]

扁平b,删除任何其他维度。
C = np.array([[1], [2], [5]])

A[np.arange(A.shape[0]),B]

A[np.arange(A.shape[0]),B.ravel()]
最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3