crscardellino - 1 year ago 63

Python Question

I am new to torch7, and I can't find a way to get the some non contiguous indices of a tensor based on another tensor. In numpy, what I do is the following:

`array = np.zeros(5) # array = [0 0 0 0 0]`

indices = np.array([0, 2, 4])

array[indices] = np.array([1, 2, 3]) # array = [1 0 2 0 3]

Is there a way to do something similar in torch7? Something like:

`array = torch.zeros(5) -- array = [0 0 0 0 0]`

indices = torch.Tensor({1, 3, 5})

array[indices] = torch.Tensor({1, 2, 3}) -- array = [1 0 2 0 3]

Thanks!

Answer

Ok, looking arround, I couldn't find an exact solution, but I found an approximation of what I wanted to do, I share it in case someone else finds it useful:

```
array = torch.zeros(5) -- array = [0 0 0 0 0]
indices = torch.LongTensor({1, 3, 5}) -- Is important that this is a LongTensor
array:indexAdd(1, indices, torch.Tensor({1, 2, 3})) -- array = [1 0 2 0 3]
```