[R] diagonal matrices

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Aug 20 19:09:57 CEST 2005


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 at miami.edu, thanks!

Please set that as your reply address to ease the lot of your helpers.

-- 
Brian D. Ripley,                  ripley at 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




More information about the R-help mailing list