这可能不是最好的解决方案,但您可以重新实现conv
矩阵,例如:
function C = convMat(A,B)
nA = size(A,3);
nB = size(B,3);
n = nA + nB - 1;
C = zeros([size(A,1),size(B,2),n]);
for k = 1:n
for j = max(1,k+1-nB):min(k,nA)
C(:,:,k) = C(:,:,k) + A(:,:,j)*B(:,:,k-j+1);
end
end
为了您的兴趣:另一个(更糟?)实现:
function C = convMat2(A,B)
n = size(A,3) + size(B,3) - 1;
C = zeros([size(A,1),size(B,2),n]);
for mA = 1:size(A,1)
for mB = 1:size(B,2)
for l = 1:size(A,2) % = size(B,1)
vA = A(mA,l,:);
vA = vA(:);
vB = B(l,mB,:);
vB = vB(:);
C(mA,mB,:) = C(mA,mB,:) + reshape(conv(vA,vB),[1,1,n]);
end
end
end
测试者:
% matrix example
A(:,:,1) = rand(3,3); % A1
A(:,:,2) = rand(3,3); % A2
B(:,:,1) = rand(3,4); % B1
B(:,:,2) = rand(3,4); % B2
B(:,:,3) = rand(3,4); % B3
C1 = convMat(A,B);
C2 = convMat2(A,B);
% test
x = rand(1,1);
(A(:,:,1) + A(:,:,2)*x) * (B(:,:,1) + B(:,:,2)*x + B(:,:,3)*x^2)
C1(:,:,1) + C1(:,:,2)*x + C1(:,:,3)*x^2 + C1(:,:,4)*x^3
C2(:,:,1) + C2(:,:,2)*x + C2(:,:,3)*x^2 + C2(:,:,4)*x^3
我确信可以优化convMat
。