Search code examples
arraysmatlabloopsmatrixbsxfun

Simplification of a complex expression


I have this complicated MATLAB expression that I would like to simplify, so that I can actually understand it.

g = repmat(log(p), [size(x, 1), 1])
    for i = 1:size(mu, 1)
        g(:, i) = g(:, i) - sum(log(sigma(i, :)));
        g(:, i) = g(:, i) - sum(bsxfun(@rdivide, bsxfun(@minus, x, mu(i, :)).^2, 2*sigma(i, :).^2), 2);
    end

p=1x2
sigma=2x2
mu=2x2
x=30x2

Basically these bsxfun functions confuse me a lot. I would like to express it in the form of simple for loops.

I tried something like this:

[m,n] = size(x)
for i=1:m
        for j=1:n
            g(i,j)= log(p(j)) - sum(log(sigma(j))) - sum(data(i,j))... ;
        end
    end

Not quite sure how to continue from this point, mostly errors and wrong results!


Solution

  • Apparently the code in the question could be used as a nice bsxfun and permute practice session to vectorize everything. I understand this is going the other way from what's asked in the question, but take it as an alternative solution.

    Coming back, to actually vectorize everything, you need to extend dimensions with permute and continue on using bsxfun. The implementation would look like this -

    %// Vectorize : "g(:, i) = g(:, i) - sum(log(sigma(i, :)))"
    parte1 = bsxfun(@minus,g,sum(log(sigma),2).'); %//'
    
    %// Vectorize : "bsxfun(@minus, x, mu(i, :)).^2"
    parte2_1 = bsxfun(@minus,x,permute(mu,[3 2 1])).^2;
    
    %// Vectorize : "bsxfun(@rdivide, ...*sigma(i, :).^2)"
    parte2 = bsxfun(@rdivide,parte2_1,2*permute(sigma,[3 2 1]).^2);
    
    %// Squeeze in dimensions of the BSXFUN extended array and subtract from parte1
    g = parte1 - squeeze(sum(parte2,2));