# Scatter sum or np.histogram with weights

Hi, what I am trying to do is the following:
I have a data array A (n, m) and an index array I of same size (n, m) and a result array R (x, n).
I am trying to scatter elements of A into R while also summing up all values which scatter to the same index.
This can be done in numpy for example in 1D arrays using np.histogram with the weights option.

This is one example in numba.cuda if it helps better explain what I want to do:

``````@cuda.jit
return
nj  = I.shape
nk = I.shape

source = int(threadidx / nk) % nj
frame = int(threadidx / (nj * nk))
target = I[frame, source, idx]
if target == -1:
return

cuda.atomic.add(R, (frame, target, 0), A[frame, source, idx, 0])
``````

Could you post some dummy values for `A`, `I` and `R`?

In this example

``````A = [[0.7,  1.3],
[56.1, 7. ]]
I = [[1, 2],
[0, 0]]
``````

Then if R is an array of zeros and shape (2, 4) it would end up being

``````R = [[0,    0.7, 1.3, 0],
[63.1, 0,   0,   0]]
``````

But this I guess is a peculiar example since I assume that each row in I corresponds to a row in A. I imagine there are different ways of storing the index in I.

I see now that A, I and R must have same length first dimension since it’s the number of samples, but can have different number of columns

You could achieve this with `tensor.scatter_add_`:

``````A = torch.tensor([[0.7,  1.3],
[56.1, 7. ]])
I = torch.tensor([[1, 2],
[0, 0]])