Is there a way I could modify the function down below so that it could compute arrays with different length sizes. the length of Numbers array is 7 and the length of the Formating is 5. The code down below compares if any number in Formating is between two values and if it the case then it sums the values that are in between. So for the first calculation since no element in Numbers is between 0, 2 the result will be 0. Link to code was derived from: issue.
Code:
Numbers = np.array([3, 4, 5, 7, 8, 10,20]) Formating = np.array([0, 2 , 5, 12, 15]) x = np.sort(Numbers); l = np.searchsorted(x, Formating, side='left') mask=(Formating[:-1,None]<=Numbers)&(Numbers<Formating[1:,None]) N=Numbers[:,None].repeat(5,1).T result= np.ma.masked_array(N,~mask) result = result.filled(0) result = np.sum(result, axis=1) Expected output:
[ 0 7 30 0] https://stackoverflow.com/questions/66593076/working-with-2-arrays-with-different-lengths-numpy-python March 12, 2021 at 09:47AM
没有评论:
发表评论