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]
N=Numbers[:,None].repeat(4,1).T. That saidxandlare redudant, and wrong!resultstuff seems overkilled.(mask * Numbers[None, :]).sum(axis=1)works fine. That said, this series of questions from OP look like an XY-problem.xandlare from a different solution,maskis what I proposed. The use of5inNis left over from whenFormattinghad 6 terms; obviously (?) ifFormatingis changed this value should as well. I suggested thenp.maas a way of visualizing the use ofmask.np.ma.sumwould have taken care of the 0 fill.