简体   繁体   English

从priority_queue获取数据后结果显示-nan

[英]Result show -nan after getting the data from priority_queue

I wrote code to assign the values to struct nngNode,but when I checked the result inside nngNode,the result displays -nan.I don't know why the -nan comes out.Before the result of nngNode,I can see the correct result from array.Here is the code:我写了代码给struct nngNode赋值,但是当我检查nngNode里面的结果时,结果显示-nan。我不知道为什么会出现-nan。在nngNode的结果之前,我可以看到正确的结果来自数组。这是代码:

1. 1.


struct nngNode{

int x;
int y;
int pixelval;
int pixelnum;
double ndist;
nngNode(int x, int y, int pixelVal,int pixelNum,double ndist) : x(x), y(y), pixelval(pixelval),pixelnum(pixelnum),ndist(ndist) {}

};

struct cmp2{

   bool operator()(nngNode &para1, nngNode &para2) {
        return para1.ndist > para2.ndist;
    }

};

template<class T1>
class Array2D{

public:
        int arows=0;
        int acols=0;
        T1**  __restrict matImg;
Array2D(int rows,int cols,T1 defaultVal):arows(rows),acols(cols){

matImg=new T1*[rows];
for(int i=0;i<rows;i++){
   matImg[i]=new T1[cols];
   memset(matImg[i],defaultVal,sizeof(T1)*cols);
   }
}

T1 &operator()(int m, const int n){

 return matImg[m][n];


 }


T1 *operator()(int k){

 return matImg[k];

}


~Array2D(){

for(int i=0;i<arows;i++){
    delete[] matImg[i];
   }
   delete [] matImg;

 }

};


int ragNode=0;
int pixelSum[id]={0};
int pixelNum[id]={0}


(After calculation the pixelSum and pixelNum have numbers.)
priority_queue<nngNode,vector<nngNode>,cmp2>nngprioq;

Array2D<double> neighDist(id,id,0);
//#pragma omp parallel for reduction(+:ragNode) 
for(int i=0;i<id;i++){
   for(int j=0;j<id;j++){
    if(idArr(i,j)==1&&pixelNum[i]>0&&pixelNum[j]>0){
    ragNode++;
   // double powerDis=((pixelSum[i]/pixelNum[i]-pixelSum[j]/pixelNum[j])/(rows*cols))*((pixelSum[i]/pixelNum[i]-pixelSum[j]/pixelNum[j])/(ros));oll
    double powerDis=pow((((double)pixelSum[i])/(double)pixelNum[i]-((double)pixelSum[j])/(double)pixelNum[j]),2);
    double mulelement=((double)pixelNum[i])*((double)pixelNum[j]);
    double divelement=(double)(pixelNum[i]+pixelNum[j]);
    cout<<"------------------------------------"<<endl;
    cout<<"powerDis:"<<powerDis<<endl;
    cout<<"mulelement:"<<mulelement<<endl;
    cout<<"divelement:"<<divelement<<endl;
    cout<<"("<<i<<","<<j<<")"<<endl;
    cout<<"psum"<<pixelSum[i]<<endl;
    cout<<"pnum"<<pixelNum[i]<<endl;
    cout<<"distance of"<<"("<<i<<","<<j<<"):"<<((mulelement*powerDis)/divelement)<<endl;
    neighDist(i,j)=((mulelement*powerDis)/divelement)+1.0;
    nngNode newnode=nngNode(i,j,pixelSum[i],pixelNum[i],neighDist(i,j));
    nngprioq.push(newnode);

       }
     }
}

Here is the result from above:(only part from the whole result)这是上面的结果:(只是整个结果的一部分)


------------------------------------
powerDis:0.681595
mulelement:47124
divelement:1711
(240,255)
psum61
pnum28
distance of(240,255):18.7724
------------------------------------
powerDis:0.425937
mulelement:26376
divelement:970
(240,268)
psum61
pnum28
distance of(240,268):11.582
------------------------------------
powerDis:4.33408
mulelement:295656
divelement:1718
(241,170)
psum1055
pnum194
distance of(241,170):745.865
------------------------------------
powerDis:8.18913
mulelement:285374
divelement:1665
(241,201)
psum1055
pnum194
distance of(241,201):1403.58
------------------------------------
powerDis:1.25224
mulelement:295462
divelement:1717
(241,247)
psum1055
pnum194
distance of(241,247):215.487
------------------------------------
powerDis:0.0334138
mulelement:25050
divelement:551
(242,219)
psum129
pnum50
distance of(242,219):1.51908
------------------------------------
powerDis:1.1664
mulelement:100
divelement:52
(242,233)
psum129
pnum50
distance of(242,233):2.24308
------------------------------------
powerDis:1.84341
mulelement:246
divelement:47
(243,251)
psum122
pnum41
distance of(243,251):9.6485
------------------------------------
powerDis:0.00629865
mulelement:46986
divelement:1187
(243,267)
psum122
pnum41
distance of(243,267):0.249325
------------------------------------
powerDis:0.0911844
mulelement:10521
divelement:522
(244,219)
psum44
pnum21
distance of(244,219):1.83784
------------------------------------


Here is the nngNode ode of priority queue,the weird thing is pixelval coming from pixelSum[i],pixelnum coming from pixelNum[i] are 1,and 0.I don't understand the why the result comes out.Because from above in array,the number of ndist,pixelval and pixelnum can be seen as result.这是优先级队列的 nngNode ode,奇怪的是 pixelSum[i] 中的 pixelval,pixelNum[i] 中的 pixelnum 是 1 和 0。我不明白为什么会出现结果。因为从上面array,ndist,pixelval 和 pixelnum 的数量可以看作是结果。

int ncounter=0;

cout<<"---------------------nng queue---------------------------"<<endl;

while(!nngprioq.empty()){
         cout<<"---------------------------------------------"<<endl;
         cout<<ncounter<<"st Element:"<<endl;
         cout<<"distanceValue:"<<(double)nngprioq.top().ndist<<endl;
         cout<<"x:"<<nngprioq.top().x<<endl;
         cout<<"y:"<<nngprioq.top().y<<endl;
         cout<<"sumPixel:"<<nngprioq.top().pixelval<<endl;
         cout<<"numPixel:"<<nngprioq.top().pixelnum<<endl;
         nngprioq.pop();
         ncounter++;
}

Here is the result with -nan:这是使用 -nan 的结果:

x:1
y:6
sumPixel:1
numPixel:0
---------------------------------------------
913st Element:
distanceValue:-nan
x:1
y:0
sumPixel:1
numPixel:0
---------------------------------------------
914st Element:
distanceValue:-nan
x:1
y:4
sumPixel:1
numPixel:0
---------------------------------------------
915st Element:
distanceValue:-nan
x:0
y:26
sumPixel:1
numPixel:0
---------------------------------------------
916st Element:
distanceValue:-nan
x:0
y:11
sumPixel:1
numPixel:0
---------------------------------------------
917st Element:
distanceValue:-nan
x:0
y:20

Could someone provide any hint or suggestions?Thanks in advance!有人可以提供任何提示或建议吗?在此先感谢!

Thank you all who share your time to review my quesion.I have found the issue which are about struct parameter and name of parameter.By changing pixelSum into pixelsum and add ndist(ndist).The issue is solved.感谢所有花时间查看我的问题的人。我发现了关于结构参数和参数名称的问题。通过将 pixelSum 更改为 pixelsum 并添加 ndist(ndist)。问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM