Try this:

m <- matrix(0, nrow = 5, ncol = 4)
diag(m) <- x[1]
diag(m[-1,]) <- x[2]

On Sat, Jul 4, 2009 at 12:17 PM, William Simpson <
william.a.simp...@gmail.com> wrote:

> Can anybody please tell me a good way to do the following?
>
> Given a vector, number of rows and number of columns, return a matrix
> as follows. Easiest to give an example:
>
> x=c(1,2), nrow=5, ncol=4
>
> return the matrix:
>
>   1 0 0 0
>   2 1 0 0
>   0 2 1 0
>   0 0 2 1
>   0 0 0 2
>
> Thanks very much for any help!
> Bill
>
> ______________________________________________
> R-help@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to