Is there a way to find number of blocks in a matrix?

3 次查看(过去 30 天)
I have a matrix that has 5 blocks along the diagonal and two consecutive blocks overlap in one diagonal element.
How can I compute the number of blocks in MATLAB?
  3 个评论
Omar B.
Omar B. 2021-6-15
As an example:
A=[1 2 0 0 0 0 0 0 0;3 4 5 6 0 0 0 0 0; 0 7 8 9 0 0 0 0 0; 0 1 2 3 4 5 0 0 0;0 0 0 1 2 3 0 0 0;0 0 0 1 2 3 4 5 0;0 0 0 0 0 1 2 3 0;0 0 0 0 0 1 2 3 4; 0 0 0 0 0 0 0 1 2]
How can I compute the number of blocks in MATLAB?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2021-6-15
编辑:Stephen23 2021-6-15
Assuming that the blocks do not contain zeros:
A = [1,2,0,0,0,0,0,0,0;3,4,5,6,0,0,0,0,0;0,7,8,9,0,0,0,0,0;0,1,2,3,4,5,0,0,0;0,0,0,1,2,3,0,0,0;0,0,0,1,2,3,4,5,0;0,0,0,0,0,1,2,3,0;0,0,0,0,0,1,2,3,4;0,0,0,0,0,0,0,1,2]
A = 9×9
1 2 0 0 0 0 0 0 0 3 4 5 6 0 0 0 0 0 0 7 8 9 0 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 1 2 3 4 5 0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 1 2 3 4 0 0 0 0 0 0 0 1 2
[R,~] = find(diff(~A,1,1)>0);
N = 1+numel(unique(R))
N = 5
If the blocks can contain zeros, then you will probably need to use some pattern matching.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by