Having an input tensor, and another tensor selection, I want to produce the output tensor as the given below

username: novice7
cooked: <p>I have the value tensor([4, 3, 6]) and index tensor([1, 1, 2])<br>
Is there some way that I could get the result tensor([[4, 3], [6,0]]) ?</p>

username: eduardo4jesus
cooked: <p>I don’t quite understand the operation you are trying to achive.<br>
Are you looking for reshaping the tensor? or creating a tensor based on a selection from a tensor of indexes? Also, where the zero came from on the resulting tensor <code>tensor([[4, 3], [6,0]])</code>?</p>

username: novice7
cooked: <p>I want to create a 2-D tensor from the value tensor and index tensor, but here the index tensor is just a 1-D or is there some way to get the resulting tensor <code>tensor([[4, 3], [6,]])</code> ?</p>

username: eduardo4jesus
cooked: <p>I am sorry, but I am not sure if I fully understand what you want yet.<br>
Is this what you are looking for?</p>
<pre><code class="lang-auto">input = torch.randint(10, (10, ))
input
# tensor([2, 9, 7, 9, 4, 4, 6, 1, 3, 3])

selection = torch.Tensor([0, 5, 4, 7])
selection
# tensor([0., 5., 4., 7.])

input[selection.long()].reshape((2, 2))
# tensor([[2, 4],
#        [4, 1]])
</code></pre>

username: novice7
cooked: <p>I am very sorry for my poor expression， if the value tensor is tensor([2, 9, 7, 8, 4])<br>
the index is tensor([0, 0, 1, 2, 3])<br>
I want to get a tensor : tensor([[2, 9], [7], [8], [4]]) or tensor([[2, 9], [7, 0], [8, 0], [4, 0]])</p>
<p>if there is still something that I did not let you understand, plz let me know, I will try another way  to express</p>
<p>maybe I need to create a zero tensor<br>
tensor([[0., 0.],<br>
[0., 0.],<br>
[0., 0.]])<br>
then the index tensor tensor([0, 0, 1, 2, 3]) is the row index</p>

username: eduardo4jesus
cooked: <aside class="quote no-group" data-username="novice7" data-post="7" data-topic="123233">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/novice7/48/39061_2.png" class="avatar"> novice7:</div>
<blockquote>
<p>index is tensor([0, 0, 1, 2, 3])</p>
</blockquote>
</aside>
<p>You are giving</p>
<pre><code class="lang-auto">input = torch.Tensor([2, 9, 7, 8, 4])
input 
# tensor([2., 9., 7., 8., 4.])

indexes = torch.Tensor([0, 0, 1, 2, 3])
indexes
# tensor([0., 0., 1., 2., 3.])

desired = torch.Tensor([[2, 9], [7, 0], [8, 0], [4, 0]])
desired
# tensor([[2., 9.],
#         [7., 0.],
#         [8., 0.],
#         [4., 0.]])
</code></pre>
<p>What does it mean to have <code>0</code> inside the <code>index</code> tensor? Here I see you having the value <code>0</code> twice. If you are selecting elements from the given tensor, <code>input = torch.Tensor([2., 9., 7., 8., 4.])</code> I would expect to see <strong>five</strong> elements been selected; and the first element, <code>input[0]</code> which is <code>2</code>, to be <strong>repeated twice</strong>. But I don’t see that on the <code>desired</code> output you gave. So, I am not following what is this operation you want.</p>
<p>Can you describe what <code>index</code> does to the <code>input</code> tensor? How come the maximum value in <code>index</code> is <code>3</code>, but the last element of the <code>input</code> tensor, which is <code>input[4]</code> with value <code>4</code> is present on the <code>desired</code> output tensor?</p>

username: eduardo4jesus
cooked: <p>Additionally, as a good practice, try to be as much precise as possible which the post subject. That way, you can lead the right people to click on your post faster.</p>

username: novice7
cooked: <p>input[4] is match the indexes[4]=3, it means, put the input[4]=4 in the third  raw of the  desired tensor</p>

username: novice7
cooked: <p>I am very appreciate for your reply!!!</p>
<p>I do want get the tensor</p>
<h1>
<a name="tensor2-9-1" class="anchor" href="#tensor2-9-1"></a>tensor([[2., 9.],</h1>
<h1>
<a name="7-0-2" class="anchor" href="#7-0-2"></a>[7., 0.],</h1>
<h1>
<a name="8-0-3" class="anchor" href="#8-0-3"></a>[8., 0.],</h1>
<h1>
<a name="4-0-4" class="anchor" href="#4-0-4"></a>[4., 0.]])</h1>
<p>input = torch.Tensor([2, 9, 7, 8, 4])</p>
<p>The index  tensor([0, 0, 1, 2, 3])  is just the raw index you can find, here I do not give the column index!<br>
From index tensor, we can find out that\ the 0th raw has the max length 2 because 0 repeated twice in index tensor , others are just 1, so in order to fill the desired tensor, there are some 0 value in desired tensor!</p>

username: novice7
cooked: <p>I also create a new post which describes my means, may be it will let you get my thought</p><aside class="quote" data-post="1" data-topic="123252">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/novice7/48/39061_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/i-have-a-value-tensor-and-a-raw-index-how-can-i-get-a-2-d-tensor/123252">I have a value tensor and a raw index, how can I get a 2-D tensor</a> 
  </div>
  <blockquote>
    the value tensor is value = torch.Tensor([2, 9, 7, 8, 4]) 
the raw index tensor = torch.Tensor([0, 0, 1, 2, 3]) 
2 matches  0  which means 2 in 0th raw, and because the desired[0][0]=2,  the 9 matches 0 but the desired[0][0]==2, so desired[0][1]=9 
I do want get the 2-D tensor 
desired tensor: 

<a name="tensor2-9-1" class="anchor" href="#tensor2-9-1"></a>tensor([[2., 9.],

<a name="7-0-2" class="anchor" href="#7-0-2"></a>[7., 0.],

<a name="8-0-3" class="anchor" href="#8-0-3"></a>[8., 0.],

<a name="4-0-4" class="anchor" href="#4-0-4"></a>[4., 0.]])
  </blockquote>
</aside>


username: eduardo4jesus
cooked: <p>Ok, I understood now what you want. I can’t see an elegant way to have a solution using plain <code>PyTorch</code>. The “naive” way to do it would be via a <code>for loop</code>.</p>
<p>First, let me redefine what I think you are asking:</p>
<hr>
<p>Having an <code>input</code> tensor, and another tensor <code>selection</code>, I want to produce the <code>output</code> tensor as the given below.</p>
<pre><code class="lang-auto">input = torch.Tensor([2, 9, 7, 8, 4])
input 
# tensor([2., 9., 7., 8., 4.])

selection = torch.Tensor([0, 0, 1, 2, 3])
selection
# tensor([0., 0., 1., 2., 3.])

desired = torch.Tensor([[2, 9], [7, 0], [8, 0], [4, 0]])
desired
# tensor([[2., 9.],
#         [7., 0.],
#         [8., 0.],
#         [4., 0.]])
</code></pre>
<p><strong>Explanation</strong></p>
<p><code>selection</code> has same size as <code>input</code> and contains the row in which each input data will be mapped into. If the row index repeats, then that next raw data input will be placed into a new column. All the other rows  are padded with zero on that case.</p>
<hr>
<p>The following assumption was not stated initially. But if it holds true, the code is more straight forward. Also, the provided example allow us to infer that.</p>
<p><strong>Assumption</strong>: <em>All rows indexes are represented in selection tensor.</em></p>
<p>As I said earlier, I can’t think of a way to solve it in an elegant manner, relying in short nested PyTorch calls. From what I know, I don’t think it is possible. But a solution can be definitely done by using a for loop.</p>
<p>solution using for loop.</p>
<pre><code class="lang-auto">unique, counters = selection.unique(return_counts=True)
n_rows, n_cols = int(torch.max(unique))+1, int(torch.max(counters.int()))

col = np.zeros((n_rows, ), dtype=int)
output = torch.zeros((n_rows, n_cols))

for data, row in zip(input, selection.tolist()):
  output[row, col[row]] = data
  col[row] += 1
  print(col)

output
# tensor([[2., 9.],
#         [7., 0.],
#         [8., 0.],
#         [4., 0.]])
</code></pre>
<hr>
<p>PS: I guess this operation you are looking for can also be explained as a sort of <code>group by</code></p>

username: eduardo4jesus
cooked: <p>I think you should be able to rename this post. So, you didn’t have to create a new one.</p>
<p>Also, I encourage you to enhance the format of your posts. There is a bar with text style options to assist you. That way people can read the content you wrote more comfortably.</p>

username: novice7
cooked: <p>OHHHHH!!! Thank u very very much!!!</p>