Last active
October 28, 2017 05:28
-
-
Save mtao/6986e35411cbc50711c9258d2bc7286e to your computer and use it in GitHub Desktop.
eigen matrix interleave by rows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Eigen/Dense> | |
| #include <tuple> | |
| #include <utility> | |
| #include <iostream> | |
| template <typename... Args, int... N> | |
| auto _interleave(std::integer_sequence<int,N...>, const Args&... args) { | |
| using namespace Eigen; | |
| constexpr static int S = sizeof...(Args); | |
| using Scalar = typename std::tuple_element<0,std::tuple<Args...>>::type::Scalar; | |
| using MatXf = Matrix<Scalar,Dynamic,Dynamic>; | |
| using MyStride = Stride<Dynamic,Dynamic>; | |
| int rows = (args.rows() + ... + 0); | |
| int cols = std::max( {args.cols()...}) ; | |
| MatXf A = MatXf::Constant(rows,cols,0); | |
| (Map<MatXf, 0,MyStride>(A.data() + N, args.rows(),args.cols(), MyStride(S*args.rows(),S)).operator=(args),...); | |
| return A; | |
| } | |
| template <typename... Args> | |
| auto interleave(const Args&... args) { | |
| return _interleave(std::make_integer_sequence<int,sizeof...(Args)>(), std::forward<const Args&>(args)...); | |
| } | |
| int main() { | |
| using RowMatXf = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>; | |
| using VecXf = Eigen::VectorXf; | |
| int N=10; | |
| VecXf A = VecXf::LinSpaced(N,0,N-1); | |
| VecXf B = A.array() + N; | |
| RowMatXf C(N,2); | |
| C.col(0) = A; | |
| C.col(1) = B; | |
| Eigen::Map<VecXf> CV(C.data(),C.size()); | |
| std::cout << A.rows() << " " << A.cols() << std::endl; | |
| std::cout << "A [\n" << A.transpose() << "\n]"<< std::endl; | |
| std::cout << "B [\n" << B.transpose() << "\n]"<< std::endl; | |
| std::cout << "C [\n" << CV.transpose() << "\n]"<< std::endl; | |
| std::cout << std::endl; | |
| C.col(0) = A.array() + 2*N; | |
| C.col(1) = A.array() + 3*N; | |
| std::cout << "C interleave [\n" << interleave(A,B).transpose() << "\n]"<< std::endl; | |
| std::cout << std::endl; | |
| std::cout << std::endl; | |
| std::cout << "Hetero interleave (A,B,C) [\n" << interleave(A,B,C).transpose() << "\n]"<< std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment