Matlab


Saving from inside a parfor loop in Matlab

If you call save from a parfor loop in Matlab, like this for example: parfor k=1:100 foo = exp(-k); save(['myfile',num2str(k),'.mat'],'foo'); end it will generate an error. I have not exactly figured out why this is but probably it has to do with the fluctuating nature of the foo variable inside the parfor loop. By calling […]

The Trace Trick for Gaussian Log Likelihood

Maybe you have seen something like this when observing the log likelihood derivations for multivariate Gaussians \( \ln p(X|\mu, \Sigma) = \frac{1}{2}\ln|\Sigma|- \frac{1}{2}X^{T}\Sigma^{-1}X + const = \frac{1}{2}\ln|\Sigma| - \frac{1}{2}Tr(\Sigma^{-1}XX^{T}) + const \) and you wondered where that \(Tr\) came from. Here you can find a great explanation but I thought I would write it down […]

Calculating the indices for n choose k

So if you want to calculate n choose k, or n over k or all k different combinations of n unique items or binomial coefficients in Matlab or in math language

\[\frac{n!}{(n-k)! k!}\]

you can write nchoosek(n,k) But lets say you want to generate a list of these possible combinations? Well then you just write […]

Debugging in Matlab

Getting in to someones code or just cleaning up your own Matlab code it's good to know how to do break points and other useful to make the process flow a bit smoother. I thought I would do a list of my favorites for making my code work faster and with less tedious work. The […]