Solved How to find a qVector <double> element from a qVector <double> index
-
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
-
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"; }
-
@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])
withif(qFuzzyIsNull(rate - x[i]))
but most of times it shows y[i] = 0;
When
i
is 0 then10 * qAbs(qSin(2.0 * M_PI * i ));
is 0 so it's a perfectly acceptable answer -
-
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(); }