Array indexing in pytorch
I am experiencing weird behavior while dealing with the following scenario. tne is the batch size, while N is the total number of possible locations for each example. Each example has exactly 24 scalar outputs, where their locations are stored in dof tensor (size of dof is (tne X 24)). Fint_e has a size of tneX24 (i.e., the 24 outputs for each example). I am trying to construct a large tensor, which has a size of tne X N. When I do the following, it fills in the wrong manner. Any advice?

Fint_MAT        = torch.zeros((tne,N))
Fint_MAT[:,dof]  = Fint_e


The dof tensor, which has the size of batch size X 24, has different indices for each example, but each example has in total 24 indices.

For instance,

dof[0,:] = 0, 1, 6, 9, … (24 in total)

dof[1,:] = 1,100, 151, 300,… (24 in total)

Any hint would be appreciated.

I include below a simple scenario for better elaboration:

tne = 3
N   = 48
Fint_MAT = torch.zeros((tne,N))
Fint_e   = torch.randn((tne, 24))
v1 = torch.arange(24).unsqueeze(0)
v2 = torch.arange(12, 36).unsqueeze(0)
v3 = torch.arange(24, 48).unsqueeze(0)
dof      = torch.cat((v1,v2,v3), axis=0).long()
Fint_MAT[:,dof]  = Fint_e


Okay, the key here is to use pairs of indices. Your dof tensor indexes the columns, but you also need to index the rows. 

x_index = torch.arange(3).unsqueeze(1).expand(3, 24)


x_index is a 3 x 24 tensor where each row is the row index. Now you can use this together with the dof tensor to index elements in the Fint_MAT matrix 

Fint_MAT[x_index, dof] = Fint_e


Basically, corresponding elements in x_index and dof form a [row, column] pair in Fint_MAT, so Fint_MAT[x_index[0], dof[0]]= Fint_e[0] etc. 

I think this should give you what you want.
