# Re: [R] diagonal matrices

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sun 21 Aug 2005 - 03:09:57 EST

On Sat, 20 Aug 2005, Afshartous, David wrote:

> I have matrices V.i of dimension n.i x n.i, where i = 1, ..., J, and the
> sum of n.i equals N. (and n.i ! = n.j)
>
> goal: create one large matrix V, where V has matrices V.i on diagonal.
>
> I create each matrix V.i in a for loop (1 to J), so each time I'd like
> to augment V with the most recently calculated V.i, such that I'll have
> V after the final iteration of the for loop.
>
> question: how to initialize V and do the augmentation. any advice much
> appreciated.

It is probably best to pre-create a matrix and fill in the blocks. As in

V <- matrix(0, N, N)
# let n be a vector of what you called n.i n0 <- c(0, cumsum(n))
for(i in 1:J) {

ind <- (n0[i]+1):n0[i+1]
V[ind, ind] <- V.i
}

>
> cheers,
> dave
>
> ps - please respond directly to afshar@miami.edu, thanks!

```--
Brian D. Ripley,                  ripley@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help