sorting along multidimensional index in numpy

Daniele Bernardini

I have a three dimensional array, that I need to sort. I also have a 2 dimensional index that I got with a lexsort that I would like to use for sorting. I cannot find how to apply the index to the array without extra dimensions sprouting up...

import numpy as np

a = np.array([
    [
        [1, 1, 1],
        [0, 1, 2],
        [3, 1, 1],
        [-1, 1, 1],
        [0, 2, 2],
        [1, 2, 2],
    ],[
        [-1, 1, 1],
        [0, 1, 2],
        [-3, 1, 1],
        [1, 1, 1],
        [0, -2, 2],
        [-1, -2, 2],
    ],[
        [-1, 1, 1],
        [-0, 1, -2],
        [-3, 1, 1],
        [1, 1, 1],
        [-0, -2, -2],
        [-1, -2, -2],
    ],
])
sorted_index = np.lexsort((a[:,:,0], a[:,:,1],a[:,:,2]),axis=1)

sorted_index is

[[3 0 2 1 4 5], [2 0 3 5 4 1], [5 4 1 2 0 3]]

the end result should be

a_sorted = np.array([
    [
        [-1, 1, 1],
        [1, 1, 1],
        [3, 1, 1],
        [0, 1, 2],
        [0, 2, 2],
        [1, 2, 2],
    ],[
        [-3, 1, 1],
        [-1, 1, 1],
        [1, 1, 1],
        [-1, -2, 2],
        [0, -2, 2],
        [0, 1, 2],
    ],[
        [-1, -2, -2],
        [0, -2, -2],
        [0, 1, -2],  
        [-3, 1, 1],
        [-1, 1, 1],
        [1, 1, 1],
    ],
])

I tried.

a[sorted_index]
a[sorted_index, :]
a[:, sorted_index]

I am about to loop through the thing, but I would like to do it properly.

meTchaikovsky

You can use np.take_along_axis

np.take_along_axis(a,sorted_index[...,None],axis=1)

the output is

array([[[-1,  1,  1],
        [ 1,  1,  1],
        [ 3,  1,  1],
        [ 0,  1,  2],
        [ 0,  2,  2],
        [ 1,  2,  2]],

       [[-3,  1,  1],
        [-1,  1,  1],
        [ 1,  1,  1],
        [-1, -2,  2],
        [ 0, -2,  2],
        [ 0,  1,  2]],

       [[-1, -2, -2],
        [ 0, -2, -2],
        [ 0,  1, -2],
        [-3,  1,  1],
        [-1,  1,  1],
        [ 1,  1,  1]]])

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get index along specific axis for multidimensional slice in numpy

NumPy nearest value along axis of multidimensional array

Sorting a NumPy array and permuting another one along with it

Sorting multidimensional array but keeping index in PHP

Javascript Sorting Multidimensional array using index

Index numpy nd array along last dimension

Numpy array assignment along axis and index

numpy apply_along_axis computation on multidimensional data

Row index's of row belongs to a DataTable along with Sorting

Numpy: Multidimensional index. Row by row with no loop

using integer as index for multidimensional numpy array

NumPy get index inside multidimensional array

Sorting each row of numpy multidimensional array in decreasing order

How to index multidimensional numpy array with another numpy array

Index of maximum values along and plane in a numpy 3D array

How to get the index of a maximum element in a numpy array along one axis

Fold out a numpy array along a new dimension using values as index

Print every row and column index for numpy matrix along with their values

How to add a vector element to every element of the subarray along the associated index of a multidimensional array

Slicing a different range at each index of a multidimensional numpy array

How to add values at repeat index locations on multidimensional arrays of Numpy?

numpy setting multidimensional arrays with index arrays containing NaNs

How to index with list of indices in a multidimensional array in numpy/pytorch

How to index multidimensional array multiple times in a vectorized way numpy?

How to get index of numpy multidimensional array in reverse order?

Sorting a Multidimensional Array is not sorting appropriately

in-place numpy array sorting according to given index

Sorting Multidimensional slices

Multidimensional array sorting issue