# [R] sub-matrix block size

Dennis Murphy djmuser at gmail.com
Wed Apr 27 06:07:00 CEST 2011

```Hi:

Maybe this can help get you started. Reading your data into a matrix m,

m <- structure(c(1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(11L,
11L))

rowSums(m) + colSums(m) - 1
[1]  2  2  1 -1  3  3  3  3  3  3 -1

The pair of 2's => a 2 x 2 block, 1 => a 1 x 1 matrix with value 1, -1
=> a 1 x 1 matrix with entry 0, a triplet of 3's => a 3 x 3 subblock,
etc. You should be able to figure out the rows and columns for each
submatrix from the indices of the vector above; the values provide an
indication of matrix size as well as position.

HTH,
Dennis

On Tue, Apr 26, 2011 at 5:13 PM, Santosh <santosh2005 at gmail.com> wrote:
> Dear Rxperts
>
> Below is a small vector of values of zeros and non-zeros... was wondering if
> there is an efficient way to get the block sizes of submatrices of a big
> matrix similar to the one shown below? diagonal elements can be zero too.
> Rows with only a diagonal element may be considered as a unit block size.
>
> c(1,0,0,0,0,0,0,0,0,0,0,
>   1,1,0,0,0,0,0,0,0,0,0,
>   0,0,1,0,0,0,0,0,0,0,0,
>   0,0,0,0,0,0,0,0,0,0,0,
>   0,0,0,0,1,0,0,0,0,0,0,
>   0,0,0,0,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,1,0,0,0,
>   0,0,0,0,0,0,0,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)
>
> Thanks much!
> Santosh
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help