」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 如何從2D數組中提取元素?使用另一數組的索引

如何從2D數組中提取元素?使用另一數組的索引

發佈於2025-05-16
瀏覽:650

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