I have a tensor `A=torch.rand(1,512,100)`

where `100`

is the duration/time in my tensor.

I have another variable `T`

that shows the duration for me, e.g., `T=torch.tensor([[20,30,40,10]])`

.

I would like to compute the average of the elements in `A`

based on the durations in `T`

so the output will be in shape of `A.shape[0],A,shape[1],T.shape[2]`

.

in other words, I want to do:

```
A=torch.rand(1,512,100)
T=torch.tensor([[20,30,40,10]])
T_cumsum =torch.cumsum(T,dim=1)
results = torch.zeros(A.shape[0],A.shape[1],T.shape[1])
results[:,:,0] = A[:,:,0:T_cumsum [0,0]].mean(2)
results[:,:,1] = A[:,:,T_cumsum [0,0]:T_cumsum [0,3]].mean(2)
results[:,:,2] = A[:,:,T_cumsum [0,1]:T_cumsum [0,2]].mean(2)
results[:,:,3] = A[:,:,T_cumsum [0,2]:].mean(2)
```

is there a way to do it (without for loops) so I can do it automatically for when `T`

has different length in dimension 1? i.e. one time `T`

can be `T=torch.tensor([[20,30,40,10]])`

and another time `T`

can be `T=torch.tensor([[50,50]])`

, the sum of `T`

elements are always equal to `A.shape[2]`