Say your array is called data:
data = [1 2 3; 1 3 2; 2 1 3; 1 0 3; 1 2 2];
You could then get the cumulative sum of values up to the first occurence of q in each row as follows:
[ ~,k ] = max(data == q,,2);
k(~any(data == q,2)) = 0
for c = 1:numel(k)
csums(c,1) = sum( data(c,1:k(c)) );
Here, data == q is a logical array of all values in data that are equal to q. Taking the max along each row will return the "first" index for a given row in data being equal to q. The second line accounts for cases where no values are equal to q.