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. How to find a qVector <double> element from a qVector <double> index
Forum Updated to NodeBB v4.3 + New Features

How to find a qVector <double> element from a qVector <double> index

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 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.
  • P Offline
    P Offline
    pedromenezes
    wrote on 16 Feb 2019, 04:06 last edited by pedromenezes
    #1

    Hi guys,
    I have two double vectors (x, y) and i need to compare the X vector element with a doubleSpin value and if they are equal I need do get the Y value at the same position.

    I tried this, but it doesn't work properly:

    QVector<double> x(512), y(512);
    double rate = prom->doubleSpinBox->value();
    double chk_rate = 0;
    
    for (int i=0; i<x.size(); ++i)
           {
           x[i] = i * 0.1;
           y[i] = 10 * qAbs(qSin(2.0 * M_PI * i ));
    if (rate == x[i]) chk_rate = y[i];
    }
    

    There is a warning : comparing floating point with == or != is unsafe!
    And besides this in sometimes the result is correct but most of times it shows y[i] = 0;

    I was trying to use this way, but i don't know how to finish:

    QVector<double> x(512), y(512);
    double rate = prom->doubleSpinBox->value();
    double chk_rate = 0;
    
    auto it = std::find(x.begin(), x.end(), rate);
    auto index = std::distance(x.begin(), it);
    chk_rate = y[index]; 
    
    

    There is other warning: implicit conversion loses integer precision: 'long' to 'int'

    Could someone help me please?

    Cheers

    V 1 Reply Last reply 18 Feb 2019, 09:01
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 16 Feb 2019, 04:31 last edited by Kent-Dorfman
      #2

      you can safely compare equivalence of floating point numbers but you have to compare the difference between them to a tolerance value. you can try something liek

      const float tolerance(0.0001f);
      if (fabs(x - y) < tolerance) {
          cout << "equivalent";
      } else {
          cout < "not equivalent";
      }
      

      I light my way forward with the fires of all the bridges I've burned behind me.

      1 Reply Last reply
      3
      • P pedromenezes
        16 Feb 2019, 04:06

        Hi guys,
        I have two double vectors (x, y) and i need to compare the X vector element with a doubleSpin value and if they are equal I need do get the Y value at the same position.

        I tried this, but it doesn't work properly:

        QVector<double> x(512), y(512);
        double rate = prom->doubleSpinBox->value();
        double chk_rate = 0;
        
        for (int i=0; i<x.size(); ++i)
               {
               x[i] = i * 0.1;
               y[i] = 10 * qAbs(qSin(2.0 * M_PI * i ));
        if (rate == x[i]) chk_rate = y[i];
        }
        

        There is a warning : comparing floating point with == or != is unsafe!
        And besides this in sometimes the result is correct but most of times it shows y[i] = 0;

        I was trying to use this way, but i don't know how to finish:

        QVector<double> x(512), y(512);
        double rate = prom->doubleSpinBox->value();
        double chk_rate = 0;
        
        auto it = std::find(x.begin(), x.end(), rate);
        auto index = std::distance(x.begin(), it);
        chk_rate = y[index]; 
        
        

        There is other warning: implicit conversion loses integer precision: 'long' to 'int'

        Could someone help me please?

        Cheers

        V Offline
        V Offline
        VRonin
        wrote on 18 Feb 2019, 09:01 last edited by
        #3

        @pedromenezes said in How to find a qVector <double> element from a qVector <double> index:

        There is a warning : comparing floating point with == or != is unsafe!

        Replace if (rate == x[i]) with if(qFuzzyIsNull(rate - x[i]))

        but most of times it shows y[i] = 0;

        When i is 0 then 10 * qAbs(qSin(2.0 * M_PI * i )); is 0 so it's a perfectly acceptable answer

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        B 1 Reply Last reply 19 Feb 2019, 06:40
        7
        • V VRonin
          18 Feb 2019, 09:01

          @pedromenezes said in How to find a qVector <double> element from a qVector <double> index:

          There is a warning : comparing floating point with == or != is unsafe!

          Replace if (rate == x[i]) with if(qFuzzyIsNull(rate - x[i]))

          but most of times it shows y[i] = 0;

          When i is 0 then 10 * qAbs(qSin(2.0 * M_PI * i )); is 0 so it's a perfectly acceptable answer

          B Offline
          B Offline
          beecksche
          wrote on 19 Feb 2019, 06:40 last edited by
          #4

          @pedromenezes

          Due to periodicity

          10 * qAbs(qSin(2.0 * M_PI * i ))
          

          is always zero.

          1 Reply Last reply
          4
          • P Offline
            P Offline
            pedromenezes
            wrote on 22 Feb 2019, 04:00 last edited by
            #5

            Thank you guys. I separated the process and it worked perfectly.

            First it generate the vector:

            void promediation::rateEffect(){
            
                int num = 512;
                int freq = 60;
              
                QString label_t = prom->label_2->text();
                double amplitude = (100-label_t.toDouble())/2;// interference adjust
                double sine = freq*2.0*M_PI;
                double r = 0;
                QSharedPointer<QCPGraphDataContainer > graphData = prom->customPlot_3->graph(0)->data();
                const double time = std::max_element(graphData->begin(),graphData->end(),
                                                      [](const QCPGraphData& a, const QCPGraphData& b)->bool{return a.key<b.key;})->key;
            
                double timewindow = time/num;
            
            
                QVector<double> x(num), y(num), y_final(num);
            
                    for (int i=0; i<x.size(); ++i)
                    {
                    r =  QRandomGenerator::global()->bounded(-100,100); //create a noise
                    x[i] = i*timewindow; // 0.0208 to show 512 points in 10.66 msec
                    double time = x[i]/1000;//time in (msec)
                    y[i] = amplitude*qSin(sine*(time)+r/1000);// Y = Amplitude*Sin(Frequency*2*Pi*time + phase)*ajust
                    }
            
                    for (int i=0; i<x.size(); ++i)
                    {
                        y_final[i] = y[i]+plot3Data->at(i)->value;
            
                    }
            }
            

            And the doubleSpinBox set the values to be plotted

            void promediation::StimRate(){
            
            
                int num = 700;// max rate 70 Hz
            
                auto plot6 = prom->customPlot_6;
                auto plotkey6 = plot6->graph(0)->data();
                double taxa = round(prom->doubleSpinBox->value()*1000.0)/1000.0;
                QString label_t = prom->label_2->text();
                double taxa_v = label_t.toDouble();
            
                QVector<double> x(num), y(num);
            
                    for (int i=0; i<x.size(); ++i)
                    {
                    x[i] = taxa;
                    y[i] = i*0.00142;
                    }
                const int PEN_WIDTH = 2;
                plot6->addGraph();
                plot6->graph(1)->setData(x,y);
                QPen iPen;
                iPen.setWidth(100);
                plot6->graph(1)->setPen(QPen(iPen));
            
            
                    if (taxa_v >= 90){
                        plot6->graph(1)->setPen(QPen(QColor(0,128,0), PEN_WIDTH));
                    }
                    if (taxa_v >= 80 and taxa_v < 90){
                        plot6->graph(1)->setPen(QPen(QColor(87,122,15), PEN_WIDTH));
                    }
            
                    if (taxa_v >= 70 and taxa_v < 80){
                        plot6->graph(1)->setPen(QPen(QColor(30,80,180), PEN_WIDTH));
                    }
            
                    if (taxa_v >= 50 and taxa_v < 70){
                        plot6->graph(1)->setPen(QPen(QColor(10,10,10), PEN_WIDTH));
                    }
                    if (taxa_v >= 20 and taxa_v < 50){
                        plot6->graph(1)->setPen(QPen(QColor(200,120,0), PEN_WIDTH));
                    }
            
                    if (taxa_v < 20){
                        plot6->graph(1)->setPen(QPen(QColor(255,0,0), PEN_WIDTH));
                    }
            
                plot6->rescaleAxes();
                plot6->replot();
            }
            
            1 Reply Last reply
            0

            1/5

            16 Feb 2019, 04:06

            • Login

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