5
我想知道是否有更好的方法來實現我在這裏做的事情。我有一個ARMA矩陣,我想通過存儲在uvec
向量中的索引對它的所有列進行重新排序。我想我基本上是複製整個矩陣。通過索引向量排序犰狳矩陣的所有列的最佳方法
#include <armadillo>
using namespace arma;
int main(){
// get a discrete random matrix
// defined umat because eventually want to
// order by a given column OF A. irrelevant now.
umat A = randi<umat>(4,6,distr_param(0,3));
std::cout << "A " << std::endl;
std::cout << A << std::endl;
// get an index vector with the now row order
uvec b;
b << 3 << 2 << 1 << 0;
std::cout << "sort by b:" << std::endl;
std::cout << b << std::endl;
// get all col indices
uvec cols = linspace<uvec>(0,A.n_cols-1,A.n_cols);
// order ALL cols of A by b
// I'm afraid this just makes a copy
A = A.submat(b, cols);
std::cout << "reordered A by b" << std::endl;
std::cout << A << std::endl;
return 0;
}