VEX : argsort() question

   400   10   1
User Avatar
Member
100 posts
Joined: Oct. 2021
Offline
Hey there.

I am currently stuck at the following thing:


I have a point cloud with several clusters. Every point in a cluster has the same cluster id.
Now I would like to sort the cluster id along the y axis.


So my idea is to:

- Group points per cluster
- Get centroids of cluster groups
- Get centroids y position ( cluster height )
- Create array of cluster heights
- Create array of cluster IDs
- Get cluster heights index array with argsort()
- Get cluster ID by height index by reorder(clusterID, clusterHieghtIndex)
- Set pointatribute @cluster_new with a for loop, i@clusters_by_height


Most of it works but the argsort() function does something I dont understand. What is it actually doing?

f@cluster_heights = 0.949269, 0.949269, 0.0507307, 0.0507307, 0.52028, 0.52028
i@cluster_heights_index = 2, 3, 4, 5, 0, 1

Should this not be: 4, 5, 0, 1, 2, 3 ???
Edited by Kareeem - yesterday 03:37:12

Attachments:
cluster_argsort_y.hip (215.5 KB)

www.rehimi.de
User Avatar
Member
5042 posts
Joined: Feb. 2012
Offline
Hi,

argsort() is working correctly. Your smallest values (0.05) are at indices 2,3, then 0.52 at 4,5, then 0.94 at 0,1. So is the right ascending order of indices.
Senior FX TD @ Industrial Light & Magic
Get to the NEXT level in Houdini & VEX with Pragmatic VEX! [www.pragmatic-vfx.com] https://lnk.bio/animatrix [lnk.bio]
User Avatar
Member
100 posts
Joined: Oct. 2021
Offline
animatrix_
Hi,

argsort() is working correctly. Your smallest values (0.05) are at indices 2,3, then 0.52 at 4,5, then 0.94 at 0,1. So is the right ascending order of indices.

hey animatrix.

maybe this was a bit unclear how i wrote but its not as you read it.

f@cluster_heights = 0.949269, 0.949269, 0.0507307, 0.0507307, 0.52028, 0.52028
i@cluster_heights_index = 2, 3, 4, 5, 0, 1

highest value 0.9 is at 2,3 ; lowest value 0.05 is at 4,5 and mi value is at 0, 1
Edited by Kareeem - yesterday 05:19:17
www.rehimi.de
User Avatar
Staff
335 posts
Joined: July 2005
Offline
Kareeem
Most of it works but the argsort() function does something I dont understand. What is it actually doing?

f@cluster_heights = 0.949269, 0.949269, 0.0507307, 0.0507307, 0.52028, 0.52028
i@cluster_heights_index = 2, 3, 4, 5, 0, 1

Should this not be: 4, 5, 0, 1, 2, 3 ???

It reports the order items should appear in a sorted list. The value at index 2 should be the first item in the sorted list, the value at index 3 should be the second item in the sorted list, etc. When you do this, the resulting sorted list is:

0.0507307, 0.0507307, 0.52028, 0.52028, 0.949269, 0.949269
User Avatar
Member
100 posts
Joined: Oct. 2021
Offline
derrick
It reports the order items should appear in a sorted list. The value at index 2 should be the first item in the sorted list, the value at index 3 should be the second item in the sorted list, etc. When you do this, the resulting sorted list is:

0.0507307, 0.0507307, 0.52028, 0.52028, 0.949269, 0.949269

Ahhh now I get it!

I thought since the array starts with the highest value the argsort() should start with the highest index as well. But it sorts it and then gets the index, so to say.

Do anybody has an idea how to get that array I am after?
Or am other way to tackle this problem as a whole?
www.rehimi.de
User Avatar
Member
504 posts
Joined: July 2005
Offline
Hi,

if you want to get the list (4, 5, 0, 1, 2, 3), just create a new int array and set new_array(arg_sort(i)) = i, where arg_sort = (2, 3, 4, 5, 0, 1 ).

Here is a modification of your file.

Attachments:
cluster_argsort_y_inverted.hipnc (214.5 KB)

User Avatar
Member
100 posts
Joined: Oct. 2021
Offline
Aizatulin
Hi,

if you want to get the list (4, 5, 0, 1, 2, 3), just create a new int array and set new_array(arg_sort(i)) = i, where arg_sort = (2, 3, 4, 5, 0, 1 ).

Here is a modification of your file.


Wow

How on earth can you wrap your head around that? That's some next level sh*t!!
An array as an array index in a for loop :o

I needed to write this out to understand this. But why would you call this "inverse"? its not really inversed the array?
Is this a known technique or did this just occurred to you?

Anyway thanks a lot. I wish only I could understand this better
Edited by Kareeem - today 05:31:01

Attachments:
arghsort_array.jpg (275.7 KB)

www.rehimi.de
User Avatar
Member
100 posts
Joined: Oct. 2021
Offline
if anyone needs this: here is my final file

Attachments:
cluster_id_by_height.hipnc (221.6 KB)

www.rehimi.de
User Avatar
Member
504 posts
Joined: July 2005
Offline
I call this "inverse" in terms of mapping. Lets say arg_sort maps (0,1,2,3,4,5) onto (2,3,4,5,0,1) can be written as (0 -> 2), (1 -> 3), (2 -> 4), (3 -> 5), (4 -> 0), (5 -> 1), what is the inverse mapping of it? It is (0,1,2,3,4,5) -> (4,5,0,1,2,3) can be written as (0 -> 4), (1 -> 5), (2 -> 0), (3 -> 1), (4 -> 2), (5 -> 3) and if we compose these function we will get (0 -> 2 -> 0), (1 -> 3 -> 1), (2 -> 4 -> 2), (3 -> 5 -> 3), (4 -> 0 -> 4), (5 -> 1 -> 5).
Like if you have a function, where finverse is the inverted function: finverse(f(x)) = x (for example log(exp(x)) = x).
Edited by Aizatulin - today 07:59:20
User Avatar
Member
100 posts
Joined: Oct. 2021
Offline
Aizatulin
I call this "inverse" in terms of mapping. Lets say arg_sort maps (0,1,2,3,4,5) onto (2,3,4,5,0,1) can be written as (0 -> 2), (1 -> 3), (2 -> 4), (3 -> 5), (4 -> 0), (5 -> 1), what is the inverse mapping of it? It is (0,1,2,3,4,5) -> (4,5,0,1,2,3) can be written as (0 -> 4), (1 -> 5), (2 -> 0), (3 -> 1), (4 -> 2), (5 -> 3) and if we compose these function we will get (0 -> 2 -> 0), (1 -> 3 -> 1), (2 -> 4 -> 2), (3 -> 5 -> 3), (4 -> 0 -> 4), (5 -> 1 -> 5).
Like if you have a function, where finverse is the inverted function: finverse(f(x)) = x (for example log(exp(x)) = x).

Thanks for taking the time to explain. This unfortunately is above my math knowledge. I have to read up on that. For example I never heard of the finverse function.


Unfortunately with a slightly more advanced setup the whole workflow doesnt seem to work. I´ll come back and post a scene ...
www.rehimi.de
User Avatar
Member
9256 posts
Joined: July 2007
Online
Kareeem
...
f@cluster_heights = 0.949269, 0.949269, 0.0507307, 0.0507307, 0.52028, 0.52028
i@cluster_heights_index = 2, 3, 4, 5, 0, 1

Should this not be: 4, 5, 0, 1, 2, 3 ???

you can also just use another argsort() to get the "inverted" indices

f[]@cluster_heights = array( 0.949269, 0.949269, 0.0507307, 0.0507307, 0.52028, 0.52028 );
i[]@indices = argsort( f[]@cluster_heights );  // [ 2, 3, 4, 5, 0, 1 ]
i[]@inverted_indices = argsort( i[]@indices ); // [ 4, 5, 0, 1, 2, 3 ]
Tomas Slancik
CG Supervisor
Framestore, NY
  • Quick Links