How do I convert an armadillo matrix to a vector of vectors?

kchow462

I created an armadillo c++ matrix as follows:

arma::mat A; 
A.zeros(3,4);

I want to convert it to a vector of vectors defined by

std::vector< std::vector<double> > B(3, std::vector<double>(4) ); 

How do I set B to equal A? If there is not an easy way for a vector of vectors, what about an array of arrays, i.e., what if I defined B to be

double B[3][4]; 
ktalik

In such cases you should use arma::conv_to which is a totally superb feature of arma.

Note that this method will require from a source object to be able to be interpreted as a vector. That is why we need to do this iteratively for every row. Here is a conversion method:

#include <armadillo>

typedef std::vector<double> stdvec;
typedef std::vector< std::vector<double> > stdvecvec;

stdvecvec mat_to_std_vec(arma::mat &A) {
    stdvecvec V(A.n_rows);
    for (size_t i = 0; i < A.n_rows; ++i) {
        V[i] = arma::conv_to< stdvec >::from(A.row(i));
    };
    return V;
}

And here is an exemplary usage:

#include <iomanip>
#include <iostream>

int main(int argc, char **argv) {
    arma::mat A = arma::randu<arma::mat>(5, 5);
    std::cout << A << std::endl;

    stdvecvec V = mat_to_std_vec(A);
    for (size_t i = 0; i < V.size(); ++i) {
        for (size_t j = 0; j < V[i].size(); ++j) {
            std::cout << "   "
                << std::fixed << std::setprecision(4) << V[i][j];
        }
        std::cout << std::endl;
    }
    return 0;
}

std::setprecision used to generate more readable output:

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

0.8402   0.1976   0.4774   0.9162   0.0163
0.3944   0.3352   0.6289   0.6357   0.2429
0.7831   0.7682   0.3648   0.7173   0.1372
0.7984   0.2778   0.5134   0.1416   0.8042
0.9116   0.5540   0.9522   0.6070   0.1567

Have a good one!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I multiply a matrix with a vector in gonum?

c++ armadillo cast/convert to integer type vector or matrix

How do I get an Armadillo function to NOT print an error when inverting a singular matrix?

How to convert a Matrix into a Vector of Vectors?

How do I convert an Armadillo matrix to cube?

How can I convert a list of character vectors to a single vector?

How do I convert (3,) numpy vector to (2,2,3) matrix?

c++, how do I copy a vector into a vector of vectors?

How do i convert a vector into armadillo matrix?

How do I interlace two Rust vectors into a new vector?

How do I convert a HashSet of Strings into a Vector?

How do I convert a vector of triplets to a 3xnx3 matrix in Dyalog APL?

How do I remove elements in 3 vectors if one vector is missing?

How do I convert vectors of various types to std::string?

How to get a vector by indexing a matrix with pair of vectors?

How do I apply an index vector over a list of vectors?

How do I print from a vector of vectors in java

How do I convert a Matrix in R to a Vector

Vector/Matrix of NaNs in armadillo?

How do I access and print individual elements of a vector of vectors?

Why does as.numeric convert a matrix to a vector, and how do I keep the result as a matrix?

How do I multiply matrix with vector and get result in matrix?

How do I convert vector to single text?

How do I convert from arrays to STL vectors?

How do I convert a list into a matrix in KDB?

How to turn a matrix of nested vectors into a vector of nested vectors

How do I transform a matrix into a vector?

How do I convert a Vector of Strings to [&str]

How do I parallelize this matrix by vector multiplication?

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive