Example #1
0
int gen_plot_kmeans(data *raw, int n, data *plot, int attempts){
	int *which;
	int i,j,k;
	double *work;
	
	extern double RunKMeansPlusPlus(int n, int k, int d, double *points, int attempts,
                 double *centers, int *assignments);

	plot->n = n;
	plot->xy = (double*)malloc(sizeof(double)*4*n);
	plot->f = plot->xy + 2*n;
	which = (int*)malloc(sizeof(int)*raw->n);
	work = (double*)malloc(sizeof(double)*2*n);
	for(i = 0; i < 2*n; ++i){
		work[i] = 0;
		plot->f[i] = 0;
	}
	RunKMeansPlusPlus(raw->n, n, 2, raw->xy, attempts, plot->xy, which);
	for(i = 0; i < raw->n; ++i){
		int c = which[i];
		double d = pythag2(plot->xy[2*c+0] - raw->xy[2*i+0], plot->xy[2*c+1] - raw->xy[2*i+1]);
		if(d > work[c]){ work[c] = d; }
	}
	
	for(i = 0; i < raw->n; ++i){
		int c = which[i];
		double d = pythag2(plot->xy[2*c+0] - raw->xy[2*i+0], plot->xy[2*c+1] - raw->xy[2*i+1]);
		if(0 == work[c]){
			d = 1.0;
		}else{
			d = 1.0 - d/work[c];
		}
		plot->f[2*c+0] += d*raw->f[2*i+0];
		plot->f[2*c+1] += d*raw->f[2*i+1];
		work[n+c] += d;
	}
	
	plot->max_len = 0;
	for(i = 0; i < n; ++i){
		if(work[n+i] > 0){
			plot->f[2*i+0] /= work[n+i];
			plot->f[2*i+1] /= work[n+i];
			double d = pythag2(plot->f[2*i+0], plot->f[2*i+1]);
			if(d > plot->max_len){ plot->max_len = d; }
			if(plot->xy[2*i+0] < plot->bbox[0]){ plot->bbox[0] = plot->xy[2*i+0]; }
			if(plot->xy[2*i+0] > plot->bbox[1]){ plot->bbox[1] = plot->xy[2*i+0]; }
			if(plot->xy[2*i+1] < plot->bbox[2]){ plot->bbox[2] = plot->xy[2*i+1]; }
			if(plot->xy[2*i+1] > plot->bbox[3]){ plot->bbox[3] = plot->xy[2*i+1]; }
		}
		//fprintf(stderr, "%f %f %f %f\n", plot->xy[2*i+0], plot->xy[2*i+1], plot->f[2*i+0], plot->f[2*i+1]);
	}
	free(work);
	free(which);
	
	return 0;
}
Example #2
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
	double *X, *centers, *v;
	int *Idx;
	float restarts, k, n,dim;
	double blah;
	
	if (nrhs == 0)	// print help info
	{
		mexPrintf("Usage: [Idx clusCentMat v] = kmeans(X, n, dim, k, restarts);\n");
		mexPrintf("where X is an array of size n*dim where X[d*i + j] gives coord j of point i\n");		
		mexPrintf("example: a=rand(10,30); [Idx, clus, v] = kmpp_mex(a, 10, 30, 3, 4);\n");
		//[Idx, clus] = kmpp_mex(a, 10, 30, 3, 4);
		return;
	}

	if (nrhs != 5){
		mexPrintf("at least five input arguments expected.");
		return;
	}
	
	X = mxGetPr(prhs[0]);
	n = (float) mxGetScalar(prhs[1]);
	dim = (float) mxGetScalar(prhs[2]);
	k = (float) mxGetScalar(prhs[3]);
	restarts = (float) mxGetScalar(prhs[4]);	
	//mexPrintf("%f %f\n", k, restarts);	

	//n = mxGetM(prhs[0]);
	//dim = mxGetN(prhs[0]);
			
	plhs[0] = mxCreateNumericMatrix((int) n, 1, mxINT16_CLASS, 0);
	plhs[1] = mxCreateNumericMatrix((int) k, (int) dim, mxDOUBLE_CLASS, 0);	
	//plhs[2] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, 0);	
	Idx = (int *)mxGetPr(plhs[0]);
	centers = mxGetPr(plhs[1]);
	//v = mxGetPr(plhs[2]);
		
	mexPrintf("n=%d;k=%d;d=%d;restarts=%d\n", (int)n, (int)k, (int)dim, (int)restarts);		
	blah = RunKMeansPlusPlus((int)n, (int)k, (int)dim, X, (int)restarts, centers, Idx);
	// debuggin info (remove it later)
	mexPrintf("%f %f %f\n", X[0], X[200], X[299]);
	mexPrintf("%d %d %d\n", Idx[0], Idx[5], Idx[9]);
	mexPrintf("%f %f %f\n", centers[0], centers[31], centers[89]);
		
	return;
}