Vectorizing Arithmetic Operations

拜拜、爱过 提交于 2019-12-11 01:49:21

问题


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.


回答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


来源:https://stackoverflow.com/questions/26598046/vectorizing-arithmetic-operations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!