[R] setting up a matrix structure

Gabor Grothendieck ggrothendieck at gmail.com
Fri Dec 23 19:26:17 CET 2005


Try this:

n <- 3; m <- 2 # test values
kronecker(diag(n), matrix(1, 1, m))

On 12/23/05, Erin Hodgess <hodgess at gator.dt.uh.edu> wrote:
> Dear R People:
>
> I have an n x (nm) matrix.
>
> In the first row, there will be n ones, followed by n(m-1) zeros.
> In the second row, there will be n zeros, n ones, and the rest zeros.
> In the third row, 2n zeros, n 1 and the rest zeros.
> .
> .
> .
> In the nth row, n(m-1) zeros and n ones.
>
> My question:  how can I do this elegantly and efficiently, please?
>
> A loop will work just fine, but there must be a better way, please.
>
> Thanks for any help!
>
> R Version 2.2.0, Windows.
>
> Sincerely,
> Erin Hodgess
> Associate Professor
> Department of Computer and Mathematical Sciences
> University of Houston - Downtown
> mailto: hodgess at gator.uhd.edu
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list