Usually when you want to do matrix algebra (since you give a link to R I suppose you want to do mathematics with that data) the best is to use a mathematics library. For example if you use eigen library it is very easy to read the matrix. After reading the matrix there are plenty of functions you can apply to these data. (check the benchmarks of this library here http://eigen.tuxfamily.org/index.php?title=Benchmark) Generally speaking if you want to do linear algebra or vector manipulation the best is to use libraries already used/tested/optimized rather to use you own std::vector<> or std::map<> and write your own loops / functions in order to manipulate the data. Your linear algebra code will not be optimized.
#include <iostream>
#include <Eigen/Dense>
int main() {
Eigen::MatrixXd m(2,3) ;
m << 1, 2, 3,
3, 3, 2;
std::cout << m << std::endl ;
std::cout << "Transpose of matrix:" << std::endl;
std::cout << m.transpose() << std::endl;
return 0 ;
}
You can compile and run it (in my system the library has been installed to /usr/local/include/eigen3/
):
$ g++ matrix.cpp -I /usr/local/include/eigen3/ -o matrix
$ ./matrix
1 2 3
3 3 2
Transpose of matrix:
1 3
2 3
3 2
For example if you want to transpose this matrix you can do it easily do it with m.transpose()
method.