This test was described in Mantel (1967) and Hubert/Schultz (1976), see links below. It's a non-parametric, permutation-based test that preserves the integrity of the structures and the mutual dependence of the observations.
The function takes three parameters X, Y and u. X and Y are matrices you wish to compare, and u is the size of the sample of possible permutations of X. If X and Y are adjacency matrices for a network, then permutations used for this test shuffle the order of the nodes only, and hence do not change the network represented by the matrix (entries on the diagonal stay on the diagonal). The total number of possible permutations is n!, which is computationally too large in most cases, and therefore X is randomly permuted u times. The assumption is that the value of D (the sum over X(i,j)*Y(i,j) for all i~=j) is normally distributed.
The output of the function is z, a two-sided normal CDF score for the correlation between X and Y.
Hubert, Schultz: http://psycnet.apa.org/?fa=main.doiLanding&uid=1978-00142-001
Create scripts with code, output, and formatted text in a single executable document.