예제 #1
0
int computeMedian(int list1[],int start1, int end1, int list2[],int start2, int end2) {

    int s1,e1,s2,e2,m1,m2;

    s1=start1;
    s2=start2;
    e1=end1;
    e2=end2;

    if((e1-s1==1)&&(e2-s2==1))
    {
        int medianSum =  findMax(list1[0],list2[0])+findMin(list1[1],list2[1]);

        return medianSum/2;
    }

    m1 = FindMedianIndex(list1,s1,e1);
    m2= FindMedianIndex(list2,s2,e2);

    if(list1[m1]== list2[m2]) {
        return list1[m1];
    }
    if(m1>m2) {
        return computeMedian(list1,s1,m1,list2,m2,e2);
    }
    else {
        return computeMedian(list1,m1,e1,list2,s1,m2);
    }

}
예제 #2
0
파일: mlesac.hpp 프로젝트: 9gel/hellopcl
template <typename PointT> double
pcl::MaximumLikelihoodSampleConsensus<PointT>::computeMedianAbsoluteDeviation (
    const PointCloudConstPtr &cloud, 
    const boost::shared_ptr <std::vector<int> > &indices, 
    double sigma)
{
  std::vector<double> distances (indices->size ());

  Eigen::Vector4f median;
  // median (dist (x - median (x)))
  computeMedian (cloud, indices, median);

  for (size_t i = 0; i < indices->size (); ++i)
  {
    pcl::Vector4fMapConst pt = cloud->points[(*indices)[i]].getVector4fMap ();
    Eigen::Vector4f ptdiff = pt - median;
    ptdiff[3] = 0;
    distances[i] = ptdiff.dot (ptdiff);
  }

  std::sort (distances.begin (), distances.end ());

  double result;
  size_t mid = indices->size () / 2;
  // Do we have a "middle" point or should we "estimate" one ?
  if (indices->size () % 2 == 0)
    result = (sqrt (distances[mid-1]) + sqrt (distances[mid])) / 2;
  else
    result = sqrt (distances[mid]);
  return (sigma * result);
}
예제 #3
0
파일: graphkmeans.cpp 프로젝트: Grabot/gmap
ClusteringInfo GraphKMeans::groupPoints(const vector<DotNode*>& means, ConnectedDotGraph& g)
{
	VVN groups = VVN(means.size(), VN());
	vector<DotNode*> median;
	for (int i = 0; i < (int)means.size(); i++)
		median.push_back(means[i]);

	for (int it = 0; it < 10; it++)
	{
		groups = VVN(median.size(), VN());

		for (int i = 0; i < (int)g.nodes.size(); i++)
		{
			double minDis = 123456789.0;
			int bestIndex = -1;

			for (int j = 0; j < (int)means.size(); j++)
			{
				double d = g.getShortestPath(g.nodes[i], means[j], true);
				if (minDis > d)
				{
					minDis = d;
					bestIndex = j;
				}
			}

			assert(bestIndex != -1);
			assert(minDis < 1234567.0);
			groups[bestIndex].push_back(g.nodes[i]);
		}

		bool progress = false;
		for (int i = 0; i < (int)median.size(); i++)
		{
			DotNode* newMedian = computeMedian(groups[i], g);
			if (median[i] != newMedian) progress = true;
			median[i] = newMedian;
		}

		if (!progress) break;
	}

	return ClusteringInfo(&g, groups);
}
예제 #4
0
int main(int argc, const char * argv[])
{

    int n ; // the number of positive integers you input,
            // it will be brought back by using call-by-reference next line.
    int * arr = readNumbers( & n );
    
    if( arr != NULL && n > 0 ) {
    
        int median = computeMedian(arr, n);
        printf("The median for the group of numbers you input is: %d \n", median );
    }
    else{
        
        printf("No positive number has been input! \n");
    }
    
    free(arr); //deallocate what arr points to.
    
    return 0;
}
예제 #5
0
파일: tests.c 프로젝트: smdarry/rdefaut2010
void testComputeMedian()
{
    ////////////////////
    // Test 1
    //
    char testData1[3][9] = { {6,1,4,3,2,4,1,6,9},
                        {13,9,11,11,10,11,12,10,11},
                        {1,0,0,1,2,1,1,2,0} };
    float median;

    printf("\nTEST MEDIAN\n");

    median = computeMedian(testData1[0], 9);
    printf("Expected: 4.0, Actual: %.1f\n", median);

    median = computeMedian(testData1[1], 9);
    printf("Expected: 11.0, Actual: %.1f\n", median);

    median = computeMedian(testData1[2], 9);
    printf("Expected: 1.0, Actual: %.1f\n", median);


    /////////////////////
    // Test 2
    //
    char testData2[3][8] = { {9,6,12,1,3,1,5,1},
                        {10,5,5,9,9,10,9,5},
                        {0,0,0,0,1,1,1,1} };
    
    median = computeMedian(testData2[0], 8);
    printf("Expected: 4.0, Actual: %.1f\n", median);

    median = computeMedian(testData2[1], 8);
    printf("Expected: 9.0, Actual: %.1f\n", median);

    median = computeMedian(testData2[2], 8);
    printf("Expected: 0.5, Actual: %.1f\n", median);
}