Search code examples
armadillo

Is there any way to turn a uvec into vec?


wanna do this:

#include <armadillo>


int main(){    
    arma::mat mat=arma::randn(5,5);    
    mat.each_col( [](arma::vec& vec){
        vec=arma::sort_index(vec);
    } );          
    return 0;

Is there any way to turn a uvec into vec?


Solution

  • Use arma::conv_to<DesiredType>::from(value).

    See below

    #include <armadillo>
    
    
    int main(){
        arma::mat mat=arma::randn(5,5);
    
        mat.print("mat");
    
        mat.each_col( [](arma::vec& vec){
                          vec = arma::conv_to<arma::vec>::from(arma::sort_index(vec));
                      } );
    
        mat.print("mat");
    
        return 0;
    }
    

    With this we can convert the output of arma::sort_index, which is an arma::uvec, into an arma::vec and the assignment will work.