Skip to content

Instantly share code, notes, and snippets.

@b0urb4k1
Forked from atandrau/covariance_matrix.cpp
Created April 16, 2018 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0urb4k1/b8cbb2eb006126d56cd5f9a06f2a2207 to your computer and use it in GitHub Desktop.
Save b0urb4k1/b8cbb2eb006126d56cd5f9a06f2a2207 to your computer and use it in GitHub Desktop.
Compute the Covariance Matrix of a 3D Point Cloud
void PointCloud::computeCovarianceMatrix() {
double means[3] = {0, 0, 0};
for (int i = 0; i < points.size(); i++)
means[0] += points[i].x,
means[1] += points[i].y,
means[2] += points[i].z;
means[0] /= points.size(), means[1] /= points.size(), means[2] /= points.size();
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
covarianceMatrix[i][j] = 0.0;
for (int k = 0; k < points.size(); k++)
covarianceMatrix[i][j] += (means[i] - points[k].coord(i)) *
(means[j] - points[k].coord(j));
covarianceMatrix[i][j] /= points.size() - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment