Index A Matrix Using A Vector of Indices in Armadillo LIbrary

1.7k views Asked by At

I'm using the Armadillo Cpp code for matrix algebra. I have an Eigenvector matrix E that I want to sort by its eigenvalues in a vector d.

mat E;
vec d;
eig_sym(d,E,Rxx);


// Sort indices of eignen values / vectors
// based on decreasing real part of eigen values.
uvec order = sort_index(-d);

// Extract top eigen vectors.
E = E(span::all,order(1,nb_sources));

I couldn't find anything related to this kind of indexing in the documentation. Indexing using a vector is such a common requirement that I'd be surprised if it isn't present in Armadillo.

What is the proper way to do this in Armadillo?

1

There are 1 answers

2
Claes Rolen On BEST ANSWER

One way to do it is

E = E.cols(order(span(0,nb_sources-1)));