Filter löschen
Filter löschen

Optimise creation of matern 3/2 covariance matrix

5 Ansichten (letzte 30 Tage)
snhah
snhah am 16 Apr. 2020
Beantwortet: darova am 17 Apr. 2020
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

Antworten (1)

darova
darova am 17 Apr. 2020
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

Kategorien

Mehr zu Sparse Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by