有没有办法对此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(行向量)
答案 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_Cell
将Y_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)