Dear R-users,
I have a matrix with a series of "not-overlapping in a row dimension" vectors
in a given structure. Something like:
|a1, 0, 0, 0|
| 0, a2, a3, 0|
|a4, 0, 0, a5|
where "ai" are column-vectors of the equal length, m.
My aim is to construct a new matrix formed by diagonal matrices built by the
mentioned vectors and placed following the original structure. Something like:
|diag(a1), 0, 0, 0|
| 0, diag(a2), diag(a3), 0|
|diag(a4), 0, 0, diag(a5)|
Of course the zeros are vectors of length m, and empty (m times m) matrices in
the first and second scheme, respectively.
I found a way to obtain what I need by selecting an augmented version of the
original matrix which I have constructed using the kronecker product. I was
wondering whether there is a more elegant and straightforward procedure.
See below a simple reproducible example of my challenge in which the length of
the vectors is 4.
Thanks in advance for your help,
Giancarlo Camarda
## size of the diagonal matrices
## or length of the vectors
m <- 4
## the original matrix
ze <- rep(0,m)
A <- cbind(c(1,2,3,4,ze,13,14,15,16),
c(ze,5,6,7,8,ze),
c(ze,9,10,11,12,ze),
c(ze,ze,17,18,19,20))
## augmenting the original matrix
A1 <- kronecker(A, diag(m))
## which rows to select
w1 <- seq(1, m^2, length=m)
w2 <- seq(0, 2*m^2, by=m^2)
w0 <- outer(w1, w2, FUN="+")
w <- c(w0)
## final matrix
A2 <- A1[w,]
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.