# b=torch.tensor([[10,5,78], [1,108,98], [90,72,2]])

b.argsort(axis=0)

## my output is : [[1, 0, 2], [0, 2, 0], [2, 1, 1]]

But correct output is after sorting is to this is below, but i haven’t got this output :
[[1, 0, 1],
[0, 2, 2],
[2, 1, 0]]

the pytorch output looks correct to me

axis=0 means for each column wise,
plzz check it again

yeah, isn’t 2 < 78 < 98?

the number tells you which element comes first, or the index of which is smallest.

so the last column, in order of 2-0-1 tells, 2nd element is smallest, followed by e-0 then e-1, which is 2-78-98.

hope this helps.

but 2-0-1 is not correct accord to all column answers
i think 1-2-0 is correct for this, but those output i have not got there

201 for last column, pls apply the same concept to the first 2 column

CL

how u can say that??
10,1,90 i got [1,0,2] # indexing in increasing order of no
5,108,72 i tgot [0,2,1]
78,98,2 i want [1,2,0] but i got [2,0,1] # now u urself calculate what u gor accord to incresing order of no sir.

What am I missing here?

As was said before:

``````[10, 1, 90] -> index: [1, 0, 2], as this will result in: [1, 10, 90]
[5, 108, 72] -> index: [0, 2, 1], as this will result in: [5, 72, 108]
[78, 98, 2] -> index: [2, 0, 1], as this will result in: [2, 78, 98]
``````

Why and how should the last index be `[1, 2, 0]`? This would result in `[98, 2, 78]`.

2 Likes

why i got [2,0,1] for [78,98,2] ???
this is wrong na?

Why is it wrong? Have a look at my example, as it shows what the sorted result will be.

Let’s maybe talk about what result do you want to get?

[78,98,2] for this in “argsort” i want [1,2,0] those i not got
why i got [2,0,1]?? bcoz it wrong for argsort

may be this explain better.

woooww greatt
u kidding me sir
do u know what ““agrsort”” do actually??
lets take 1 instead o 5 in 2nd column ,it doesnot effect the output 1 is alsi less than => 108,72
[1,108,72] so according to ur logic it may be [1,2,0] but output is [0,2,1]
means ur logic is failed sir
first undesrstand what “argsort” do then make ur logic sir
thank you

`torch.` `argsort` ( input , dim=-1 , descending=False , out=None ) → LongTensor

Returns the indices that sort a tensor along a given dimension in ascending order by value.

This is the second value returned by `torch.sort()` . See its documentation for the exact semantics of this method.

Parameters

• input ( Tensor ) – the input tensor.
• dim ( int , optional ) – the dimension to sort along
• descending ( bool , optional ) – controls the sorting order (ascending or descending)

Example:

a = torch.randn(4, 4)
a
tensor([[ 0.0785, 1.5267, -0.8521, 0.4065],
[ 0.1598, 0.0788, -0.0745, -1.2700],
[ 1.2208, 1.0722, -0.7064, 1.2564],
[ 0.0669, -0.2318, -0.8229, -0.9280]])

torch.argsort(a, dim=1)
tensor([[2, 0, 3, 1],
[3, 2, 1, 0],
[2, 1, 0, 3],
[3, 2, 1, 0]])

``
understood and pasted from torch.argsort

Dear brother @AnilNITT ,
if u would have known what argsort does you would have understood, after these many explanations atleast. Your mindset is wrong.

1 Like

torch.argsort(a, dim=1)
tensor([[2, 0, 3, 1],
[3, 2, 1, 0],
[2, 1, 0, 3],
[3, 2, 1, 0]])

dim=1 means row-wise argument sort
can u explain me how u got [2,0,3,1] for 1st row [ 0.0785, 1.5267, -0.8521, 0.4065] ???
bcoz we given index according to increasing order value in all other rows.
if u explain it i got my question answer too bro @Prashant_Kalikotay
@ptrblck @tancl @SimonW

I’m not sure at this moment, if you are trolling or why the ascending ordering of these numbers is not clear enough.

Let’s take the example you are mentioning:

``````x = [ 0.0785, 1.5267, -0.8521, 0.4065]
``````

The output (as given by @Prashant_Kalikotay) is

``````[2,0,3,1]
``````

which gives the ordering:

``````[x[2], x[0], x[3], x[1]] = [-0.8521, 0.0785, 0.4065, 1.5267]
``````

since

``````-0.8521 < 0.0785 < 0.4065 < 1.5267
``````

Which part is confusing you?

1 Like

Like @ptrblck, I think taking a step back and noticing that no less than four kind people have been going out of their way trying to help might be useful. Keeping things friendly seems not only nice but also essential to your cause and the forums in general.

My impression is that the confusion is between the permutation mapping unordered to ordered (returned by argsort) and the inverse, mapping ordered to unordered.

In the 2d case, you can invert the permutation using scatter (in 1d, indexed assignment `c[b] = torch.arange(...)` is quicker and more obvious) if you need the inverse.

``````a = torch.randn(4,4)
b = a.argsort(dim=1)

c = torch.scatter(torch.zeros_like(b), 1, b, torch.arange(b.size(1))[None, :].expand_as(b))

# check that c is the inverse of b
print(b.gather(1, c))
# c, the inverse permutation of what argsort returns,  probably is what you are looking for
print(c)

# or you can get the sorted values and rearrange to match the original using the inverse permutation
print((a.gather(1, b)==a.sort(1).values).all().item())
print(((a.sort(1).values).gather(1, c)==a).all().item())
``````

Best regards

Thomas

1 Like