有没有办法对此循环进行矢量化

时间:2016-06-17 07:08:00

标签: performance matlab for-loop vectorization

有没有办法对此for循环进行矢量化以加快速度?

谢谢

        for j =1 :size(Rond_Input2Cell,1)

            for k=1: size(Rond_Input2Cell,2)

                Rond_Input2Cell(j,k)=  (Pre_Rond_Input2Cell(j,k)*Y_FGate(k))+(net_Cell(k)*Y_InGate(k)*tmp_input(j)) ;

            end
        end

P.S。

矩阵大小:

Rond_Input2Cell = 39 * 120

Pre_Rond_Input2Cell = 39 * 120

Y_FGate = 1 * 120(行向量)

net_Cell = 1 * 120(行向量)

Y_InGate = 1 * 120(行向量)

tmp_input = 1 * 39(行向量)

2 个答案:

答案 0 :(得分:1)

您可以在不使用for loop的情况下加速此计算,而是使用bsxfun使用内存来加速处理 下面的代码逐行执行相同的功能并添加它们

 Rond_Input2Cell = bsxfun(@times,tmp_input.' ,net_Cell.*Y_InGate) +  bsxfun(@times ,Pre_Rond_Input2Cell,Y_FGate);

Exlpanation:

Pre_Rond_Input2Cell(j,k)*Y_FGate(k)

这是通过使用bsxfun(@times ,Pre_Rond_Input2Cell,Y_FGate)执行的,Y_FGate将每行39行Pre_Rond_Input2Cell与120列net_Cell(k)*Y_InGate(k)*tmp_input(j)

进行多次删除

bsxfun(@times,tmp_input.' ,net_Cell.*Y_InGate)tmp_input取代,net_CellY_InGate的每个元素与Rond_Input2Cell>> perform_check Elapsed time is 0.000475 seconds. Elapsed time is 0.000156 seconds. >> perform_check Elapsed time is 0.001089 seconds. Elapsed time is 0.000288 seconds. 的点复制相互叠加。最后,它存储在repmat中{1}}

这是性能检查

tic;
    Rond_Input2Cell =(Pre_Rond_Input2Cell.*repmat(Y_FGate,size(Pre_Rond_Input2Cell,1),1)) + (repmat(tmp_input.',1,size(Pre_Rond_Input2Cell,2)).*repmat(net_Cell.*Y_InGate,size(Pre_Rond_Input2Cell,1),1));
    toc;

另一种方法是使用for

>> perf_test
Elapsed time is 0.003268 seconds.
Elapsed time is 0.001719 seconds.
>> perf_test
Elapsed time is 0.004211 seconds.
Elapsed time is 0.002348 seconds.
>> perf_test
Elapsed time is 0.002384 seconds.
Elapsed time is 0.000509 seconds.

以下是使用repmat循环

的性能测试
bsxfun

Here是Loren关于$min = $this->createQueryBuilder('t1') ->select('MIN(t1.id)') ->where('t.id = t1.id' ) ->andWhere('t1.id > :id') ->andWhere('t1.type = :type'); $max = $this->createQueryBuilder('t2') ->select('MAX(t2.id)') ->where('t2.id > :id') ->andWhere('t2.type = :type') ->andWhere('t2.id = t.id'); return $this->createQueryBuilder('t') ->addSelect('(' . $min->getDQL() . ') AS min') ->addSelect('(' . $max->getDQL() . ') AS max') ->setParameter('id', $id) ->setParameter('type', $type) ->getQuery() ->getScalarResult(); vs $project = DB::table('projects') ->join('assigned_projects' , 'projects.id' , '=', 'assigned_projects.project_id') ->where('assigned_projects.user_id', '=', 9) ->get(); 的表现的文章

答案 1 :(得分:0)

你的矢量化代码应该是这样的。

temp_mat = tmp_input' * (net_Cell .* Y_InGate) - 尺寸(39 * 120)

Rond_Input2Cell = (Pre_Rond_Input2Cell .* Y_FGate) .+ temp_mat - 尺寸(39 * 120)