Sums of a ragged array


I have a ragged array in the form of a vector of values, vals, and a corresponding vector of indices, ind. Both of these vectors are of length n. The elements of ind are in the range 1:k.

I want to increment a k-vector of sums, sums, with the values of vals according to inds, as in

for (v, i) in zip(vals, inds)
    sums[i] += v

Is there a sum method like this?

Writing this out explicitly is obviously not very difficult but I have the feeling that I am missing an abstraction here.

sums += vals[invperm(inds)]

should do it.


I’m afraid that won’t work because inds is not necessarily a permutation. In most cases k < n.

I ended up creating a RaggedArray struct with the two fields (is the terminology “properties”, as in getproperty and setproperty!, now?) and defining the sum method on that.