Matlab中的块矩阵内部产品

时间:2019-02-20 09:45:13

标签: matlab matrix linear-algebra

我一直在使用以下自定义函数来将向量与矩阵相乘,其中向量的每个元素都将(3xN)x(3)矩阵内的3x3块相乘:

function [B] = BlockScalar(v,A)

 N=size(v,2);
 B=zeros(3*N,3);

 for i=1:N
     B(3*i-2:3*i,:) = v(i).*A(3*i-2:3*i,:);
 end

end

类似地,当我要将3x3矩阵的集合乘以3x3向量的集合时,我使用以下内容

function [B] = BlockMatrix(A,u)

 N=size(u,2);
 B=zeros(N,3);

 for i=1:N
     B(i,:) = A(3*i-2:3*i,:)*u(:,i);
 end

end

由于我经常调用它们,因此不幸的是,这些操作大大降低了我的代码的运行速度。我想知道上述操作是否有更有效的(也许是矢量化的)版本。

2 个答案:

答案 0 :(得分:2)

在这两种情况下,您都可以取消for循环(尽管未经测试,但我无法确认这是否一定会加快您的计算速度)。

对于第一个功能,您可以执行以下操作:

function [B] = BlockScalar(v,A)
% We create a vector N = [1,1,1,2,2,2,3,3,3,...,N,N,N]
N=ceil((1:size(A,1))/3); 

% Use N to index v, and let matlab do the expansion
B = v(N).*A;

end

对于第二个函数,我们可以制作一个块对角矩阵。

function [B] = BlockMatrix(A,u)

 N=size(u,2)*3;
 % We use a little meshgrid+sparse magic to convert A to a block matrix
 [X,Y] = meshgrid(1:N,1:3);

% Use sparse matrices to speed up multiplication and save space
 B = reshape(sparse(Y+(ceil((1:N)/3)-1)*3,X,A) * (u(:)),3,size(u,2))';

end

请注意,如果您能够访问各个3x3矩阵,则可以通过使用本机blkdiag来使此操作更快/更简单:

function [B] = BlockMatrix(a,b,c,d,...,u)
% Where A = [a;b;c;d;...];

% We make one of the input matrices sparse to make the whole block matrix sparse
% This saves memory and potentially speeds up multiplication by a lot
% For small enough values of N, however, using sparse may slow things down.
reshape(blkdiag(sparse(a),b,c,d,...) * (u(:)),3,size(u,2))';

end

答案 1 :(得分:0)

以下是矢量化解决方案:

function [B] = BlockScalar(v,A)
    N = size(v,2);
    B = reshape(reshape(A,3,N,3) .* v, 3*N, 3);
end


function [B] = BlockMatrix(A,u)
    N = size(u,2);
    A_r = reshape(A,3,N,3);
    B = (A_r(:,:,1) .* u(1,:) + A_r(:,:,2) .* u(2,:) + A_r(:,:,3) .* u(3,:)).';
end

function [B] = BlockMatrix(A,u)
    N = size(u,2);
    B = sum(reshape(A,3,N,3) .* permute(u, [3 2 1]) ,3).';
end