[R] matrix problem

William Simpson william.a.simpson at gmail.com
Sat Jul 4 18:26:16 CEST 2009


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<jorgeivanvelez at 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.simpson at 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 at 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.
>
>




More information about the R-help mailing list