2

I am trying to improve the performance of my code by converting some iterations into matrix operations in Matlab. One of these is the following code and I need to figure out how can I avoid using loop in the operation.

Here gamma_ic & bow are two dimensional matrices. c & z are variables set from outer iterations.

for z=1:maxNumber,
    for c=1:K,
        n = 0;
        for y2=1:number_documents,
            n = n+(gamma_ic(y2,c)*bow(y2,z));
        end
        mu(z,c) = n / 2.3;
    end
end

Appreciate your assistance.

Edit. Added The loop for c and z. The iteration goes on till the maximum indices in gamma_ic & bow. Added mu which is another two dimensional matrix to show usage of n.

5
  • Do c and z iterate through all possible indices for gamma_ic and bow respectively? I think it is worth showing how they iterate, and how you are using n. Also any thoughts or understanding you have about vectorisation (it may be easy or hard to vectorise your code, but it would be good to understand what is blocking you and try to fix that) Commented Oct 27, 2014 at 22:43
  • If you share the loop codes for c and z, we might be able to help to get rid of those loops too. But for the given code, you could try out - n = sum(gamma_ic(1:number_documents,c).*bow(1:number_documents,z)) Commented Oct 27, 2014 at 22:48
  • I have updated the code with relevant portions of the outer loops. Commented Oct 27, 2014 at 22:50
  • What exactly or how exactly are you getting maxNumber and K? Commented Oct 27, 2014 at 22:52
  • maxNumber and K are constants being passed on to the function Commented Oct 27, 2014 at 22:53

1 Answer 1

1

This should work for you to get mu, which seems to be the desired output -

mu = bow(1:number_documents,1:maxNumber).'*gamma_ic(1:number_documents,1:K)./2.3
Sign up to request clarification or add additional context in comments.

1 Comment

I'm a bit confused here, will the dimensions agree to perform element wise operation? Note that K may not be equal to maxNumber. Edit: got it, that's why you are taking the transpose I suppose? Ignored it initially

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.