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/