Skip to content

Instantly share code, notes, and snippets.

@brandones
Last active June 16, 2016 16:53
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 brandones/995c53f527e924e788c2 to your computer and use it in GitHub Desktop.
Save brandones/995c53f527e924e788c2 to your computer and use it in GitHub Desktop.
Matlab implementation of PCA on a graph
function X = graph_pca(A, k)
% A: the adjacency matrix of a graph
% k: the number of dimensions to reduce to
%
% Calculates the ECTD-preserving PCA of the graph given by A.
% See http://outobox.cs.umn.edu/PCA_on_a_Graph.pdf for background.
L = diag(sum(A)) - A;
Lp = pinv(L);
[U, E] = eigs(Lp, k);
X = E.^(1/2) * U';
endfunction
@brandones
Copy link
Author

So this is actually a very silly and inefficient way to do this, since inv(eigs(L)) == eigs(pinv(L)). Inverting L, which is mad expensive, is not actually necessary. See the python version for an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment