簡體   English   中英

在matlab中計算對角塊狀矩陣的逆矩陣

[英]Calculate the inverse matrix of a diagonal blockwise matrix in matlab

我有一個像這樣的大矩陣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,B6A1,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()

現在,我的電腦與大多數電腦有點不同,所以你可能想重新做這個測試。 但是這些比率大致相同 - 計算明確的反轉只需要比計算產品x = inv(A)*b花費更多的時間。

如果在使用mldivide時內存mldivide ,則不應遍歷所有單個矩陣,而是將問題分解為更大的塊。 像這樣的東西:

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

對角矩陣的倒數僅為B = 1 / A.

這里的證明: http//www.proofwiki.org/wiki/Inverse_of_Diagonal_Matrix

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM