c++ - Best way to sort all columns of an armadillo matrix by an index vector -
i'm wondering whether there's better way achieve i'm doing here. have arma matrix , want reorder of it's columns indices stored in uvec
vector. think i'm copying whole matrix.
#include <armadillo> using namespace arma; int main(){ // discrete random matrix // defined umat because want // order given column of a. irrelevant now. umat = randi<umat>(4,6,distr_param(0,3)); std::cout << "a " << std::endl; std::cout << << std::endl; // index vector row order uvec b; b << 3 << 2 << 1 << 0; std::cout << "sort b:" << std::endl; std::cout << b << std::endl; // col indices uvec cols = linspace<uvec>(0,a.n_cols-1,a.n_cols); // order cols of b // i'm afraid makes copy = a.submat(b, cols ); std::cout << "reordered b" << std::endl; std::cout << << std::endl; return 0; }
you right in code creates new matrix , not exchange rows in place.
alternatively express permutation product of transpositions , swap rows of a
one-by-one swap_rows
. of course not trivial implement , go route if memory usage of concern or if need permute few of rows , leave rest are. otherwise rebuilding matrix faster due cache efficiency.
for example case, reverses row order, might of course want swap last , first row, last-1'th , 2nd , on.
Comments
Post a Comment