Home MATLAB: summing values in matrix up to a threshold level of one column

# MATLAB: summing values in matrix up to a threshold level of one column

user5818
1#
user5818 Published in September 21, 2018, 8:03 am

So, I have a matrix with 2 columns and 5 rows (as an example).

2 1
5 1
3 1
4 1
7 1


what I want to do is:

Starting from position (1,1) and moving down the first column, find the cells that lead to a value <10. In this case I would have:

step 1: 2 = 10? No, continue
step 2: 2+5 = 10? No, continue
step 3: 2+5+3 = 10? Yes, stop and return the sum of the corresponding values in the second column
step 4: 4 = 10? No, continue
step 5: 4+7 = 10? No, it's larger, thus we save the previous step and return 1 form the second column.


In the end of this process I would need to obtain a new matrix that looks like this:

10 3
4 1
7 1

• If you had a 2, say, in your first column between the 4 and 7, would the output be then expected to contain 6 2 instead of 4 1 or (as @Wolfie 's answer currently does) both rows from the input matrix? – etmuse Feb 14 at 9:22