How do i create a matrix of complex numbers in Eigen

c++, eigen, matrix

Solution

Here is a simpler version of your code making a better usage of Eigen:

int cols = 100;
int rows = 100;
MatrixXf Shapes(rows, 2*cols);
MatrixXcf X(rows, cols);
X.real() = Shapes.leftCols(cols);
X.imag() = Shapes.rightCols(cols);
X.array().colwise() -= X.rowwise().mean().array();

Problem

I have a matrix sized NxM and would like to create a matrix of complex numbers of size N/2 x M where the real numbers are the left side of the matrix and the complex part is the right side. I came up with this: ``` auto complexmatrix= Shapes.block(0,0,Shapes.rows(),data.cols()) * std::complex<float>(1,0) + Shapes.block(0,data.cols(),Shapes.rows(),data.cols())*std::complex<float>(0,1); std::cout << complexmatrix<< std::endl; ``` Can this be optimized or are there a better way to create the complex matrix. All in all, the code ended up like this. Feels like i am missing something from Eigen. The goal was to convert to Complex notation and subtract the row-wise mean from each row. ``` //Complex notation and Substracting Mean. Eigen::MatrixXcf X = Shapes.block(0,0,Shapes.rows(),data.cols()) * std::complex<float>(0,1) + Shapes.block(0,data.cols(),Shapes.rows(),data.cols())*std::complex<float>(1,0); Eigen::VectorXcf Mean = X.rowwise().mean(); std::complex<float> *m_ptr = Mean.data(); for(n=0;n<Mean.rows();++n) X.row(n) = X.row(n).array() - *m_ptr++; ```

Original source