Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Converting Matrix column element to Qvector
Forum Updated to NodeBB v4.3 + New Features

Converting Matrix column element to Qvector

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Warrior4just
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • W Offline
        W Offline
        Warrior4just
        wrote on last edited by
        #3

        SGalst thanks for your reply

        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->addGraph();
        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();
        

        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can try something simpler like:

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • W Offline
            W Offline
            Warrior4just
            wrote on last edited by
            #5

            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 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6
              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)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved