Converting Matrix column element to Qvector

• hello
i would like to convert Matrix object of the Eigen library into Qvector. Is there any other way to do it rather than iterators (foeach).

Thanks a lot

• Hi,

I don't know Eigen very well but you could try to copy the raw data from the matrix into the vector

what i wanted to do is just draw a random white noise set of data, using Eigen library and Qcustomplot library

The portion of the code used is as follow;

@
MatrixXd A = MatrixXd::Random(N,N);
VectorXd b = VectorXd::Random(N);

VectorXd xx = VectorXd::Zero(N);

for (int i=0;i<xx.size();i++)
{
xx(i)=i;
}

VectorXd yy = A.colPivHouseholderQr().solve(b);

QVector<double> x,y;

for (int i=0;i<xx.size();i++)
{
x[i]=xx(i);
y[i]=yy(i);
}

ui->widplot->graph(0)->setData(x, y);

ui->widplot->xAxis->setLabel("x");
ui->widplot->yAxis->setLabel("y");

ui->widplot->xAxis->setRange(-10, 10);
ui->widplot->yAxis->setRange(10, 10);
ui->widplot->replot();

@

• You can try something simpler like:

@
QVector<double> y(N);
memcpy(y.data(), yy.data(), sizeof(double) * N);
@

• SGalst
I appreciate it man...
It worked, but i dont understand two things if you don't mind clarifying

1- the third parameter in memcpy (sizeof(double)* N), the definition says the size of the byte of data that needs to be copied from source to dest, but how is this (sizeof (double) * ) equivalent to the description....i see a pointer to a number of type sizeof. i dont see it

2-
why didnt
@
for (int i=0;i<xx.size();i++)
{
x[i]=xx(i);
y[i]=yy(i);
}
@

work in my code above...

Je vous remercie...

1. Like you read, memcpy copies bytes of data from one point to the other. The default type of your matrix and hence your QVector is double, a double is more than one byte (depending on your platform it's e.g. 4). So in order to copy the correct amount of data you need to tell memcpy to copy your N elements that are the size in bytes of a double.

2. You are trying to access an element of a QVector that has no size. You can use
@ x << xx(i);@

which will append to x the value at xx(i)