Optimise creation of matern 3/2 covariance matrix

5 次查看(过去 30 天)
I'm making a function to create a Matern 3/2 covariance matrix (see below). The size of the matrix varies but is typically with n >= 1e5. My attempt currently takes very long.
How can I increase the performance of this code?
function K = matern32(n,tau)
% Function creates n x n matern covariance matrix
K = sparse(n,n);
eps = 1e-9;
for i = 1:n
for j = 1:n
K(i,j) = (1+sqrt(3)*abs(i-j)/tau)*exp(-sqrt(3)*abs(i-j)/tau);
if K(i,j) < eps
K(i,j) = 0;
end
end
end
end

回答(1 个)

darova
darova 2020-4-17
Try this
[X,Y] = meshgrid(1:n);
K = (1+sqrt(3)*abs(X-Y)/tau).*exp(-sqrt(3)*abs(X-Y)/tau);
K = (K>1e-9).*K;
read this

类别

Help CenterFile Exchange 中查找有关 Sparse Matrices 的更多信息

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by