How to identify blocks in a diagonal block matrix?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ludovic Tenorio
am 24 Aug. 2023
Bearbeitet: Stephen23
am 25 Aug. 2023
I have the following type of block diagonal matrix whose non-overlapping "blocks" I would like to indentify. These blocks may vary in size and may contain zeros.
M = [
1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 1 0 0
0 0 0 0 0 0 0 0 1 1 1 1
0 0 0 0 0 0 0 1 1 1 0 1
0 0 0 0 0 0 0 0 1 0 1 1
0 0 0 0 0 0 0 0 1 1 1 1]
For this specific example, there are 4 distinct blocks whose indices are:
1, 2, 3 (3x3 block)
4, 5, 6 (3x3 block)
7 (1x1 block)
8, 9, 10, 11, 12 (4x4 block)
2 Kommentare
Paul
am 25 Aug. 2023
What's the rule for identifying a distinct block? For example, why isn't 7 - 12 considered a single 5 x 5 block?
Akzeptierte Antwort
Stephen23
am 25 Aug. 2023
Bearbeitet: Stephen23
am 25 Aug. 2023
This is not very pretty, but it gets the job done. Note for simplicity it only handles square matrices and assumes square, non-overlapping blocks of data. Extending this approach to non-square matrices and blocks might be possible.
M = [1,1,1,0,0,0,0,0,0,0,0,0; 1,1,1,0,0,0,0,0,0,0,0,0; 1,1,1,0,0,0,0,0,0,0,0,0; 0,0,0,1,1,1,0,0,0,0,0,0; 0,0,0,1,1,1,0,0,0,0,0,0; 0,0,0,1,1,1,0,0,0,0,0,0; 0,0,0,0,0,0,1,0,0,0,0,0; 0,0,0,0,0,0,0,1,0,1,0,0; 0,0,0,0,0,0,0,0,1,1,1,1; 0,0,0,0,0,0,0,1,1,1,0,1; 0,0,0,0,0,0,0,0,1,0,1,1; 0,0,0,0,0,0,0,0,1,1,1,1];
char(M+'0') % just for compact display of the entire matrix
assert(isequal(diff(size(M)),0),'matrix must be square')
C = {};
B = 1; % block begin
E = B; % block end
R = size(M,1);
while B<R
while E<R && any(any(M(E+1:R,B:E)|M(B:E,E+1:R).'))
E = E+1;
end
C{end+1} = M(B:E,B:E); %#ok<SAGROW>
E = E+1;
B = E;
end
Checking:
C
C{:}
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!