If all your matrices are so large, and you execute your for-loop only 4 times, then there is no reason to remove the for-loops, since it will not lead to any speedup. My only observation is that the condition of the if
seems to be independent of the loop, so it is cleaner to move that if
before the loop. Something like this:
if isfield(model, 'gamma') && ~isempty(model.gamma)
myKuu = model.KuuGamma;
else
myKuu = model.Kuu;
end
logDetKuu = 0;
for r=1:model.nlf,
[model.Kuuinv{r}, model.sqrtKuu{r}] = pdinv(myKuu{r});
model.logDetKuu{r} = logdet(myKuu{r}, model.sqrtKuu{r});
logDetKuu = logDetKuu + model.logDetKuu{r};
end