The vecnorm function probably does some error- and type-checking which you don't need, and you can combine two square root operations into one. This might be faster:
coef = (x.' * y) / sqrt(sum(x.^2) * sum(y.^2));
Given that this code is no longer hinged to vecnorm, could you then parallelize your calls? For example,
N = 20000000;
n = 10;
x = rand(n,N);
y = rand(n,N);
c = corr_(x,y);
where the modified correlation function is now
function coef = corr_(x, y)
n = size(x, 1);
x = x - sum(x, 1)/n;
y = y - sum(y, 1)/n;
coef = sum(x .* y) ./ sqrt(sum(x.^2) .* sum(y.^2));
end
That gives a 10x speedup for me.
Best Answer