gpt4 book ai didi

performance - Matlab 中更快的矩阵递归

转载 作者:行者123 更新时间:2023-12-02 06:54:47 25 4
gpt4 key购买 nike

n x n 矩阵 Y_t 的矩阵递归如下所示:

Y_{t} = A + \sum_{i=1}^{p} B_{i} * Y_{t-i}

给出了A和B。

这是我的尝试,但运行缓慢:

Y = zeros(n,n,T); %Going to fill the 3rd dimension for Y_t, t=1:T
Y(:,:,1:p) = initializingY

for t=(p+1):T
Y(:,:,t) = A;
for i=1:p
Y(:,:,t) = Y(:,:,t) + B(:,:,i)*Y(:,:,t-i);
end
end

你能想出更有效的方法吗?

最佳答案

你可以用matrix-multiplication终止内部循环经过一些reshaping & permuting,像这样-

Y = zeros(n,n,T);
%// Y(:,:,1:p) = initializingY
for t=(p+1):T
Br = reshape(B(:,:,1:p),n,[]);
Yr = reshape(permute(Y(:,:,t-(1:p)),[1 3 2]),[],n);
Y(:,:,t) = A + Br*Yr;
end

关于performance - Matlab 中更快的矩阵递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33673273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com