在Matlab中更快的矩阵递归

sto*_*enm 2 recursion performance matlab matrix

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

Y_{t} = A + \sum_{i=1}^{p} B_{i} * Y_{t-i}
Run Code Online (Sandbox Code Playgroud)

给出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
Run Code Online (Sandbox Code Playgroud)

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

Div*_*kar 5

你可以用一些&之后杀死内循环,就像这样 -matrix-multiplicationreshapingpermuting

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
Run Code Online (Sandbox Code Playgroud)