0

I was trying to create a vector from the non-zero entries of a sparse matrix and thought that the 'vectorise' function would do the trick.

The Armadillo documentation indicates that the 'vectorise' function has supported the sparse matrix format since version 9.400, however when I try to compile the code below I get an error:

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export]]
arma::vec flatten_matrix(){
    arma::sp_mat A = arma::sprandu(1000, 2000, 0.01);
    arma::vec B = arma::vectorise(A);
    return B;
}

error: conversion from 'arma::enable_if2, arma::spop_vectorise_col> >::result {aka const arma::SpOp, arma::spop_vectorise_col>}' to non-scalar type 'arma::vec {aka arma::Col}' requested arma::vec B = arma::vectorise(A);

My Armadillo installation comes via the RcppArmadillo R package, which I have at version 0.9.700.2.0.

Am I the problem, or is this a bug?

frank2165
  • 114
  • 2
  • 7
  • To create a vector from the non-zero entries of a sparse matrix, use the [`nonzeros()`](http://arma.sourceforge.net/docs.html#nonzeros) function instead: `arma::vec B = arma::nonzeros(A);`. For sparse matrices, the [`vectorise()`](http://arma.sourceforge.net/docs.html#vectorise) function returns a sparse vector. – hbrerkere Sep 11 '19 at 12:21
  • Thanks @hbrerkere. I don't know why I fixated on vectorise. – frank2165 Sep 12 '19 at 02:03

0 Answers0