How to create a matrix of complex numbers in Eigen

I have an NxM size with the dimensions of a matrix and would like to create an N / 2 x M matrix of complex numbers where the real numbers are the left side of the matrix and the hard side 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;

      

Could this be optimized or is there a better way to create a complex matrix.

In general, the code turned out like this. Feels like I'm missing something from Eigen. The goal was to convert to complex notation and subtract the average of the string from each string.

//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++;

      

+3


source to share


1 answer


Here's a simpler version of your code to make better use 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();

      

+4


source







All Articles