我有一个像这样的大矩阵M
M=[A1, Z, Z, Z, Z, Z ;
Z, A2, Z, Z, Z, Z ;
Z, Z, A3, Z, Z, Z ;
Z, Z, Z, A4, Z, Z ;
Z, Z, Z, Z, A5, Z ;
Z, Z, Z, Z, Z, A6];
A1,A2,A3,A4,A5,A6
是 4×4 实对称矩阵,Z=zeros(4,4)
。
当矩阵 A1,A2,A3,..., An
中有数百万个 A
时,如何计算 M
的倒数>?
我知道我可以将逆矩阵简化为
invM=[B1, Z, Z, Z, Z, Z
Z, B2, Z, Z, Z, Z
Z, Z, B3, Z, Z, Z
Z, Z, Z, B4, Z, Z
Z, Z, Z, Z, B5, Z
Z, Z, Z, Z, Z, B6];
B1,B2,B3,B4,B5,B6
是A1,A2,A3,A4,A5,A6
的逆矩阵。但是当B
很多的时候,如何进行批处理呢?
提前谢谢你。
最佳答案
坦率地说,我不会为倒数烦恼。很可能您根本不需要反转,而是您需要产品
x(n) = inv(A(n))*b(n)
其中 b
是方程 Ax = b
中的解向量。
这就是为什么这很重要:
clc
N = 4; % Size of each A
m = 300; % Amount of matrices
% Create sparse, block diagonal matrix, and a solution vector
C = cellfun(@(~)rand(4), cell(m,1), 'UniformOutput', false);
A = sparse(blkdiag(C{:}));
b = randn(m*N,1);
% Block processing: use inv()
tic
for ii = 1:1e3
for jj = 1:m
inds = (1:4) + 4*(jj-1);
inv(A(inds,inds)) * b(inds); %#ok<MINV>
end
end
toc
% Block processing: use mldivide()
tic
for ii = 1:1e3
for jj = 1:m
inds = (1:4) + 4*(jj-1);
A(inds,inds) \ b(inds);
end
end
toc
% All in one go: use inv()
tic
for ii = 1:1e3
inv(A)*b;
end
toc
% All in one go: use mldivide()
tic
for ii = 1:1e3
A\b;
end
toc
结果:
Elapsed time is 4.740024 seconds. % Block processing, with inv()
Elapsed time is 3.587495 seconds. % Block processing, with mldivide()
Elapsed time is 69.482007 seconds. % All at once, with inv()
Elapsed time is 0.319414 seconds. % All at once, with mldivide()
现在,我的 PC 与市面上的大多数 PC 略有不同,因此您可能需要重新进行此测试。但比率大致相同——计算显式逆比计算乘积 x = inv(A)*b
花费的时间要多得多。
如果您在使用 mldivide
时内存不足,您不应该遍历所有单独的矩阵,而是将问题分成更大的 block 。像这样:
chunkSize = N*100;
x = zeros(size(A,1),1);
for ii = 1:N*m/chunkSize
inds = (1:chunkSize) + chunkSize*(ii-1);
x(inds) = A(inds,inds) \ b(inds);
end
关于matlab - 在matlab中计算对角分块矩阵的逆矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812561/