An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110426/de3ea096/attachment.pl>
sub-matrix block size
6 messages · Santosh, Peter Alspach, Dennis Murphy +1 more
Tena koe Santosh It is not clear to me precisely what your blocking rules are (e.g., where should matrix[4,4] go, is matrix[5:7,5:7] to be considered as a different block to matrix[8:10,8:10]). Also, what happens if there is an isolated 1 (e.g., in location matrix[9,6])? However, I imagine something could be achieved with judicious use of diag() and rle(). HTH .... Peter Alspach
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of Santosh Sent: Wednesday, 27 April 2011 12:14 p.m. To: r-help Subject: [R] sub-matrix block size 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 PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On Apr 27, 2011, at 12:07 AM, Dennis Murphy wrote:
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.
If we are in the stage of providing potentially useful but incomplete ideas, this would be my notion. Use the row and col functions with "[" to locate non-zero elements in the diagonal and subdiagonal: Diagonal: (My matrix was named `mm`) > mm[row(mm)==col(mm)] [1] 1 1 1 0 1 1 1 1 1 1 0 First subdiagonal: > mm[row(mm)==col(mm)+1] [1] 0 0 0 0 0 0 0 0 0 0 First superdiagonal: > mm[row(mm)==col(mm)-1] [1] 1 0 0 0 1 1 0 1 1 0 Perhaps a combination of the two? It seems as though the rowSums/ colSums approach might be insensitive to whether triangular blocks were sub or super diagonal: > rowSums(mm) + colSums(mm) - 1 [1] 2 2 1 -1 3 3 3 3 3 3 -1 > mm[1,2]<-0 > mm[2,1]<-1 > rowSums(mm) + colSums(mm) - 1 [1] 2 2 1 -1 3 3 3 3 3 3 -1
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 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD West Hartford, CT
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110427/2c39f848/attachment.pl>
2 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110429/2436552a/attachment.pl>