This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Sorting Matrix

Tags: None
(comma "," separated)
clockdate
Registered Member
Posts
2
Karma
0

Sorting Matrix

Fri Jan 18, 2013 9:23 am
Is there an answer to sort a matrix itself by specific row as follows?

MATLAB example:
Code: Select all
>> A=[33.3 -11.1 44.4 55.5 22.2
      16.3 -99.1 62.4 41.5 75.2
      93.3 -32.1 37.4 33.5 18.2
      20.3 -58.1 12.4 63.5 28.2
      87.3 -23.1 86.4 82.5 36.2
      66.3 -19.1 78.4 16.5 55.2]

A =
   33.3000  -11.1000   44.4000   55.5000   22.2000
   16.3000  -99.1000   62.4000   41.5000   75.2000
   93.3000  -32.1000   37.4000   33.5000   18.2000
   20.3000  -58.1000   12.4000   63.5000   28.2000
   87.3000  -23.1000   86.4000   82.5000   36.2000
   66.3000  -19.1000   78.4000   16.5000   55.2000

>> [header order] = sort(A(1,:))

header =
  -11.1000   22.2000   33.3000   44.4000   55.5000

order =
     2     5     1     3     4

>> table = A(:, order)

table =
  -11.1000   22.2000   33.3000   44.4000   55.5000
  -99.1000   75.2000   16.3000   62.4000   41.5000
  -32.1000   18.2000   93.3000   37.4000   33.5000
  -58.1000   28.2000   20.3000   12.4000   63.5000
  -23.1000   36.2000   87.3000   86.4000   82.5000
  -19.1000   55.2000   66.3000   78.4000   16.5000


Eigen example:
Code: Select all
// SortTest.cpp

MatrixXd A(6,5);
MatrixXd B(6,5);

A << 33.3,-11.1, 44.4, 55.5, 22.2,
     16.3,-99.1, 62.4, 41.5, 75.2,
     93.3,-32.1, 37.4, 33.5, 18.2,
     20.3,-58.1, 12.4, 63.5, 28.2,
     87.3,-23.1, 86.4, 82.5, 36.2,
     66.3,-19.1, 78.4, 16.5, 55.2;

// B = sortcolumns(A, 0);  // I NEED A SOLUTION LIKE THIS!
B = A.sort_columns_by_row(0);  // I NEED A SOLUTION LIKE THIS!

cout << "Here is the sorting result by first row of a matrix A itself:\n";
cout << B;



C:\File\SortTest\Debug>SortTest.exe
Here is the sorting result by first row of a matrix A itself:
  -11.1  22.2  33.3  44.4  55.5
  -99.1  75.2  16.3  62.4  41.5
  -32.1  18.2  93.3  37.4  33.5
  -58.1  28.2  20.3  12.4  63.5
  -23.1  36.2  87.3  86.4  82.5
  -19.1  55.2  66.3  78.4  16.5


Can you help me?

Last edited by clockdate on Sat Jan 19, 2013 10:29 am, edited 2 times in total.
fbeyer
Registered Member
Posts
8
Karma
0

Re: Sorting Matrix

Fri Jan 18, 2013 12:48 pm
You can achieve that using a custom comparator for std::sort:
Code: Select all
#include <Eigen/Core>
#include <iostream>

using namespace Eigen;

template <typename Derived>
struct Comparator
{
    const Derived& matrix;
    const int row;

    Comparator(const MatrixBase<Derived>& m, int r)
        : matrix(m.derived()), row(r)
    {}

    bool operator()(int i, int j) const
    {
      return matrix(row, i) < matrix(row, j);
    }
};

template <typename Derived>
VectorXi columnArgSort(const MatrixBase<Derived>& matrix, int row)
{
    // Start with indices [0 1 2 ... N-1]
    VectorXi indexes = VectorXi::LinSpaced(matrix.cols(), 0, matrix.cols() - 1);

    // Sort using our custom comparator, that will compare matrix values indirectly.
    std::sort(indexes.data(), indexes.data() + indexes.size(),
              Comparator<Derived>(matrix, row));
   
    return indexes;
}

int main()
{
  MatrixXd A(6,5);
  MatrixXd B(6,5);

  A << 33.3,-11.1, 44.4, 55.5, 22.2,
       16.3,-99.1, 62.4, 41.5, 75.2,
       93.3,-32.1, 37.4, 33.5, 18.2,
       20.3,-58.1, 12.4, 63.5, 28.2,
       87.3,-23.1, 86.4, 82.5, 36.2,
       66.3,-19.1, 78.4, 16.5, 55.2;

  VectorXi order= columnArgSort(A, 0);
  std::cout << "order =\n" << order.transpose() << "\n\n";

  for (int i = 0; i < order.size(); ++i)
  {
    B.col(i) = A.col(order[i]);
  }

  std::cout << "table =\n" << B << std::endl;
 
  return 0;
}

(Note that, to keep things simple, I used int instead of typename Derived::Index for the index data type.)

columnArgSort returns "order", the vector of indexes that would sort the columns; the loop uses those to copy the columns in the correct order.

Output:
Code: Select all
$./a.out
order =
1 4 0 2 3

table =
-11.1 22.2 33.3 44.4 55.5
-99.1 75.2 16.3 62.4 41.5
-32.1 18.2 93.3 37.4 33.5
-58.1 28.2 20.3 12.4 63.5
-23.1 36.2 87.3 86.4 82.5
-19.1 55.2 66.3 78.4 16.5


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]