Try this:

foo <- function(x, nrow, ncol){
    m <- matrix(0, nrow = nrow, ncol = ncol)
    m[cbind(unlist(lapply(0:(ncol - 1), `+`, seq(x))),rep(1:ncol, each =
length(x)))] <- x
    m
}

foo(c(2, 1, 1, 1), nrow = 5, ncol = 2)


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

> Doesn't work:
>
> > Make(x=c(2,1,1,1),nR=5,nC=2)
>     [,1] [,2]
> [1,]    2    0
> [2,]    1    2
> [3,]    0    1
> [4,]    0    0
> [5,]    0    0
>
> should be
>     [,1] [,2]
> [1,]    2    0
> [2,]    1    2
> [3,]    1    1
> [4,]    1    1
> [5,]    0    1
>
>
>
> On Sat, Jul 4, 2009 at 5:04 PM, Jorge Ivan
> Velez<jorgeivanve...@gmail.com> wrote:
> > Dear William,
> >
> > Here is one way using Henrique's solution:
> >
> > Make <- function(x, nR, nC){
> >      m <- matrix(0, nrow = nR, ncol = nC)
> >      diag(m) <- x[1]
> >      diag(m[-1,]) <- x[2]
> >      m
> >     }
> >
> > Make(x = c(1,2), nR = 5, nC = 4)
> >
> > HTH,
> >
> > Jorge
> >
> >
> > On Sat, Jul 4, 2009 at 11:59 AM, William Simpson
> > <william.a.simp...@gmail.com> wrote:
> >>
> >> Thanks everyone for the help.
> >>
> >> I should have said that I want to do this generally, not as a one-off.
> >> So I want a function to do it. Like this
> >>
> >> tp<-function(x, nr, nc)
> >> {
> >> matrix( c(x,rep(0, nr-length(x)+1)),  nrow=nr, ncol=nc)
> >> }
> >>
> >> tp(x=c(1,2), nr=5, nc=4)
> >>
> >> This one looks good -- the warning message is annoying though...
> >>
> >>
> >> 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.
> >
> >
>
> ______________________________________________
> 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