efficient covariance matrix

This is a discussion on efficient covariance matrix within the C Programming forums, part of the General Programming Boards category; hello, i have a routine to calculate the 3x3 covariance matrix of a nx3 matrix, however it's not very efficient ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    9

    efficient covariance matrix

    hello,
    i have a routine to calculate the 3x3 covariance matrix of a nx3 matrix, however it's not very efficient as it uses 3 nested for loops, and with large data sets it really clogs up. i was wondering if anyone could offer any advice to speed it up at all, the code is below, cheers,

    Code:
    double** getcovariancemat(Object* o, Object* p, int ndims)
    {
    	int i, j, k;
    
    	//malloc space for cov-matrix here
    	
    	Vertex* omean = (Vertex*) malloc(sizeof(Vertex));
    	Vertex* pmean = mean(p);
    
    	//number of vertices in the data object (the subsampled object)
    	int pverts = p->nvertices;
    	Vertex* ann[pverts];
    	void* kdtree = buildkdtree(o);
    	
    	//create an array of pverts size filled with nearest neighbors of 
    	//vertices of p contained in o
    	for (i = 0; i < pverts; ++i) {
    		ann[i] = nearestneighbour(&(p->vertices[i]), (struct kdtree*)kdtree);
    		omean->x += (ann[i]->x);
    		omean->y += (ann[i]->y);
    		omean->z += (ann[i]->z);
    		//printf("x: %g, y: %g, z: %g, \n",ann[i]->x, ann[i]->y, ann[i]->z);
    	}
    
    	omean->x /= pverts;
    	omean->y /= pverts;
    	omean->z /= pverts;
    	
    	//arrays for calculating the variances from the data sets
    	double modelvar[pverts][3];
    	double datavar[pverts][3];
    	
    	for (i = 0; i < pverts; ++i) {
    		modelvar[i][0] = (p->vertices[i].x - pmean->x);
    		modelvar[i][1] = (p->vertices[i].y - pmean->y);
    		modelvar[i][2] = (p->vertices[i].z - pmean->z);
    		datavar[i][0] = (ann[i]->x - omean->x);
    		datavar[i][1] = (ann[i]->y - omean->y);
    		datavar[i][2] = (ann[i]->z - omean->z);
    	}
    	
    	//calculating the covariance matrix - not very efficient O(n^3)
    	for (i=0; i< ndims; ++i) {
    		for (j = 0; j < ndims; ++j) {
    			for (k=0; k < pverts; ++k) {
    				double val = (modelvar[k][i]*datavar[k][j]);
    				m[i][j] += val;
    			}
    			m[i][j] /= (pverts);
    		}
    	}
    	
            for (i = 0; i < pverts; ++i) {
    		free(ann[i]);
    	}
    	free(pmean);
    	free(omean);
    	return m;
    }
    Last edited by qualia; 08-26-2010 at 06:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21