向量化算术运算 [英] Vectorizing Arithmetic Operations

查看:97
本文介绍了向量化算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过将一些迭代转换为Matlab中的矩阵运算来提高代码的性能.其中之一是以下代码,我需要弄清楚如何避免在操作中使用循环.

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.

此处gamma_ic&弓是二维矩阵. c& z是从外部迭代中设置的变量.

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

感谢您的帮助.

编辑.添加了c和z的循环.迭代一直进行到gamma_ic& amp;中的最大索引为止.弓.添加了mu,这是另一个二维矩阵,用于显示n的用法.

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.

推荐答案

这应该对您有用,以获得mu,这似乎是所需的输出-

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

这篇关于向量化算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆