MATLAB: How to find the elements in an array that the summation of the elements equals to certain value

ismembersum

Hi.
I need help to find MATLAB coding that can find the elements in array in which the summation of those elements equal to certain value in another column in the same array. For example :
A=[0 21;21 3;0 4; 7 3;10 2; 0 5]
The programme should
1)find the summation value of elements in column 2 that equals to nonzero value in column 1.
2) store the index of elements in column 2 in the last value of the summation occur.
Example : A=[ 0 21
21 3
0 4
7 3
10 2
0 5 ]
first non zero value in column 1 is 21, therefore 21 equals to first element in Column 2 => index 2 will be stored to Column 3 row 1
second non zero value in column 1 is 7, therefore equals to 3+4 (2nd and 3rd element) in Column 2 => index 4 will be stored to Column 3 row 3 (after the last element for summation which is 4).
third non zero value in column 1 is 10, therefore equals to 3+2+5 (4th,5th,6th element) in Column 2 => index 5 will be stored to Column 3 row 6 (after the last element for summation which is 5).
Thanks

Best Answer

Are you looking for something like this
A=[ 0 21;
21 3;
0 4;
7 3;
10 2;
0 5];
idx_col1 = find(A(:,1));
uni_col1 = A(idx_col1, 1);
edges = [0; cumsum(uni_col1)];
sum_col2 = cumsum(A(:,2));
A(:,3) = idx_col1(discretize(sum_col2-1, edges));