Trying to return an std::vector
from a designated column in a matrix. This is the code I have so far:
template <typename T>
vector<T> ExtractMatrixColAsVector(NumericMatrix x, NumericVector column){
vector<T> values = as<vector<T> >(NumericVector(x(_,as<int>(column))));
return values;
}
I was wondering whether there was a better way of doing this if I wanted to convert the whole matrix into separate vectors? Is there an iterator for this purpose or some syntactic sugar that returns a vector of that column automatically?
Thanks for any help.
You could use a quick for loop to convert the whole matrix.
// [[Rcpp::export]]
vector< vector<double> > ExtractMatrixAsVectors(NumericMatrix x){
vector< vector<double> > values(x.nrow());
for(int i=0; i<values.size(); i++) values[i] = as< vector<double> >(NumericVector(x(_,i)));
return values;
}
Also, I don't see too much point in using a template. The output of a numeric matrix column will always be a double precision float.