基于索引数组从二维数组中提取元素 [英] Pulling elements from 2D array based on index array

查看:78
本文介绍了基于索引数组从二维数组中提取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

import numpy as np
A = np.arange(15).reshape(3, 5)

我还有一个索引数组:

ind = np.asarray([1,2,0,2,2])

ind的元素代表A的每一列的A的行号.

The elements of ind represent the row number of A for each column of A.

我想从 A 的第 0 列中提取 ind[0] = 1 元素我想从 A

I want to pull ind[0] = 1 element from column 0 of A I want to pull ind[4] = 2 element from column 4 of A

所需的输出是:

5, 11, 2, 13, 14

推荐答案

使用 Numpy 的 fancy-indexing -

A[ind,np.arange(ind.size)]

这篇关于基于索引数组从二维数组中提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆