Search code examples
arraysmatlabsumintervalsaccumarray

Sum of elements in a vector using intervals


Let's say I have a vector named vect = [1 2 3 4 5 6 7 8 9] and another vector named intervals = [1 3 6 9]. Is it possible to get another vector of partial sums of elements in vest, using intervals? I want to do something like this:

Partial sum 1 = 1 + 2 + 3 (elements from vect(1) to vect(3))

Partial sum 2 = 4 + 5 + 6 (elements from vect(3 + 1) to vect(6))

Partial sum 3 = 7 + 8 + 9 (elements from vect(6 + 1) to vect(9))

So what I want to do is to obtain the sum of the first k elements, then the sum of another k elements starting from the first one not in the previous sum etc.

Original problem: The original problem was like this: I receive a vector with n values and a value k. Let's do t = max(v) / k intervals. Now, how many values from n are in the interval [0, t)? What about [t, t * 2)? What about [2 * t, 3 * t)? etc. Until now, I used accumarray(v, 1) to find how many values of each do I have and int = (0:max(v)/k:max(v)) to build the invervals vector, but now I have to sum(accumarray(v, 1)) in a way to get those partial sums.

If you want to test, using this (http://pastebin.com/jCZ3qYhz : generated with accumarray) and k = 16, the partial sums have to be: 157, 167, 136, 251, 726, 1300, 1152..I'd like to be able to do this without a for/while loop :) Vectorization is the key!

Edit: To get my first sum, I use this: sum(accumarray(v, 1)(1:16)), for the second one: sum(accumarray(v, 1)(17:32)), but I don't know how to vectorize this operation. I tried this: i = (1:16:500). Then sum(accumarray(v, 1)(i(1:length(i)) : i(2:length(i))) but it's not really working or I don't use the right thing.


Solution

  • For the first problem I would use this:

    cs = cumsum(vect);
    i2 = intervals(2:end);
    result = [cs(i2(1)), diff(cs(i2))]
    
    result =
    
         6    15    24
    

    This will build the cumulative sum of all elements, find the ends of the parts, and then find the differences between the sums at the end of the parts (which are precisely the sums of the elements in-between).