hi all,

I have this minor problem which is making me go cranky at the mo.. Can't think straight,
need some insights to what I can do...
Below is the code which I am working on which takes the test sample values stored in my test 2D array subtracts the centroids stored in centroid 2D array and stores it in temp1 1D array where temp1 goes from 1 to 4. The values in temp1 array are now squared, summed up and storee inside temp1 array again and does this for 2308 times (CLEN). I want to know how I can increment my h index without using another for loop. Where do I initialize it. Before the i for loop or before j for loop?? Please help!!!

for(int i=0;i<TestNum;i++){
         for(int j=0;j<CLEN;j++){
                temp1[h]=test[i][j]-centroid[h][j];
                temp1[h]=temp1[h]*temp1[h];
                temp1[h]+=temp1[h];

         }

        temp1[h]=sqrt(temp1[h]);
        sim[EWS][i]=1/(1+temp1[h]);

        temp1[h]=sqrt(temp1[h]);
        sim[BL][i]=1/(1+temp1[h]);

        temp1[h]=sqrt(temp1[h]);
        sim[NB][i]=1/(1+temp1[h]);
        //cout<<sim[NB][i]<<"\t";

        temp1[h]=sqrt(temp1[h]);
        sim[RMS][i]=1/(1+temp1[h]);
        //cout <<sim[RMS][i]<<"\t";



     //Getting the maximum similarity from the 4 for each test samples
	maxSim = EWS;   //max =0
	for(int p=1;p<NumClass;p++)
             if(sim[p][i] > sim[maxSim][i])
                 maxSim = p;

	if(t_val[i]==maxSim)
             correctClassCount++;

}

Recommended Answers

All 2 Replies

hi all,

I have this minor problem which is making me go cranky at the mo..

Do other problems make you a little Larry, and others Curly? :icon_rolleyes:

Below is the code which I am working on which takes the test sample values stored in my test 2D array subtracts the centroids stored in centroid 2D array and stores it in temp1 1D array where temp1 goes from 1 to 4. The values in temp1 array are now squared, summed up and storee inside temp1 array again and does this for 2308 times (CLEN). I want to know how I can increment my h index without using another for loop. Where do I initialize it. Before the i for loop or before j for loop?? Please help!!!

Depends on what you are trying to accomplish. Since you calculate values and put them all in temp[h] , it's' difficult to know what you are doing. Maybe an example of the first 12 or 16 temp values (with description) would make your intentions clear.

Thanks for the reply WaltP.

My program is a classification program. I've managed to calculate and store the centroids of 4 classes found in a 63 x 2308 dimension data file. It stores those in centroid[4][2308]. Now, it takes the test samples, stored in test[20][2308] and tries to calculate it's euclidean distance and euclidean similarities. Euclidean Distance is calculated as the sum of all test[20][2308]-centroid[4][2308] raised to the power of 2. When I posted the above code, I was wrong by putting all of those calculations in temp[h] as WaltP has pointed clearly. I could have simply done something like the one as shown below. I just wanted to store the values of temp[h] so that the h goes up till 4 times only so it stores the distance values of the 4 classes for all 20 test samples. But this problem is being solved simply by just adding another for loop in between the two existing for loops I already had... I was just trying to tidy up my codes to remove the unnecessary repetition of calculating the square root of the distances that I found inside the inner for loop.

Here's my code:

for(int i=0;i<TestNum;i++){  //Loops 63 times
     maxSim = EWS; //maxSim = 0;
     for(int f=0;f<NumClass;f++){ //Loops 4 times
          for(int j=0;j<CLEN;j++){ //Loops 2308 times

/*Calculates distance of each test sample to a particular class*/
                if(mask[j]==1)
                     dist += pow(test[i][j]-centroid[f][j],2);  //euclidean distance
	  }        
	  temp1[f]= 1/(1+sqrt(dist)); //calculates euclidean similarity here

//finds maximum similarity for all 4 classes in each 20 samples            
	  if(temp1[f] > temp1[maxSim]) 
             maxSim = f;
					
	dist = 0;
     }
//counts the number of test samples that are being correctly classified from the 20 //samples 		
	if(t_val[i]==maxSim)
        correctClassCount++;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.