I am searching around for a while, but still can not solve the problem I have at hand. Could somebody point me to the solution, or give an suggestion to this question?
I have a tensor,
A = [[row1], B = [1, 2, 1]
[row2],
[row3]]
tensor B indicates which group does each row of A belong to, and showing the group number
Question:
How can I rearrange A, such that rows for the same group can be grouped together, like:
A_after_rearrange = [[row1]
[row3],
[row2]]
If I correctly understood your question, this would work. If you want a more general way, you can easily extend it with a loop. It was not a matter of PyTorch, just Python programming…
import torch
a = torch.tensor([[1,2,3],[4,5,6],[7,8,9]])
b = torch.tensor([1,2,1])
You misunderstood me: I just meant that you should use a for loop but only if you have to do more than two replacements and just for compactness. I also would say that how vector b is built is rather counterintuitive but I don’t know from where it comes. I can guess some class labels. However, if instead of having b like this you manage to have b = [0,2,1], which gives the indeces where you want to place the rows, there exists a builtin method that does the job. Try, with b = [0,2,1]:
If I am using torch.sort(b) then the internal order of all 1s in b is disturbed. It means the inds=[2, 0, 1], not inds=[0, 2, 1]. Is there a way to get b sorted but not disturb elements internal ordering?
_, inds = torch.sort(b) will give inds = [2, 0, 1]. If I want inds = [0, 2, 1], which means the ordering between two 1s stay the same, then how should I do?