简体   繁体   English

二维数组与一维数组的比较返回二维数组

[英]2d array compare to 1d array returns 2d array

I am trying to compare a 1D array element-wise to a 2D array, and returns the elements of the 2D array which fulfils the condition in a 2D array form without using a for loop.我试图将一维数组元素与二维数组进行比较,并返回二维数组中满足条件的二维数组元素,而不使用 for 循环。 Preferably using numpy or quicker method.最好使用 numpy 或更快的方法。

a = range(1,10)
Tna = np.random.choice(a, size=[250,10,1000], replace=True)
sum_Ta = np.sum(Tna, axis = 1)
percent = np.percentile(sum_Ta, 5, axis =0)

Now I would like to get a 2D array which contains the elements of sum_Ta if the elements are smaller the percent.现在我想得到一个包含 sum_Ta 元素的二维数组,如果元素小于百分比。 Such that 250 elements of sum_Ta are comparing with 1 element of percent for 1000 times.这样 sum_Ta 的 250 个元素与 1 个百分比元素比较 1000 次。 Originally I can do, ES = sum_Ta[sum_Ta < percent[:,None]] , but it only gives me a 1D array, not a 2D array.最初我可以做, ES = sum_Ta[sum_Ta < percent[:,None]] ,但它只给我一个一维数组,而不是一个二维数组。

Assuming you mean that for each row, you want the element of the row to be included if it is less than the percentage associated with its column.假设您的意思是对于每一行,如果该行的元素小于与其列关联的百分比,则您希望包含该行的元素。

Try the following:请尝试以下操作:

mask = sum_Ta < (percent * np.ones((250,1)))
ES = np.zeros((250, 1000))
ES[mask] = sum_Ta[mask]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM