且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

向量化一个嵌套循环,其中一个循环变量依赖于另一个

更新时间:2022-12-08 23:17:21

您对上一个问题的已接受解决方案的评论之一,您提到了基于连续 bsxfun(@times,..,permute..) 的代码更快.如果是这种情况,您也可以在这里使用类似的方法.这是使用这种模式以及 tril -

In one of your comments to the accepted solution of the previous question, you mentioned that successive bsxfun(@times,..,permute..) based codes were faster. If that's the case, you can use a similar approach here as well. Here's the code that uses such a pattern alongwith tril -

B1 = tril(bsxfun(@times,B,[1 ones(1,numel(B)-1).*2]));
v1 = bsxfun(@times,B1, permute(C,[3 2 1]));
v2 = bsxfun(@times,v1, permute(I,[4 3 2 1]));
A = bsxfun(@times,A, permute(v2,[5 6 4 3 1 2]));