1

I want to construct a matrix of dimension T x T. In the first row and in the last column I want all zeros. Further, from second row up to row T and from the first column up to column T-1, I want an identity matrix. In the case where T=4, it should look something like this:

        1. column    2. column   3. column  4. column
1. row:     0           0           0           0

2. row:     1           0           0           0

3. row:     0           1           0           0

4. row:     0           0           1           0

I hope it makes sense,

Thanks.

1 Answer 1

2

Have a look at ? diag, rbind, and ?cbind:

n <- 4
rbind(rep(0, n), cbind(diag(1, n-1), rep(0, n-1)))

     [,1] [,2] [,3] [,4]
[1,]    0    0    0    0
[2,]    1    0    0    0
[3,]    0    1    0    0
[4,]    0    0    1    0

(Note that T is often used as a shortcut for TRUE so you should avoid it as a variable name or you are going to have some problems...)

1
  • 2
    can also do rbind(0, diag(1, n-1, n))
    – flodel
    Commented Nov 22, 2013 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.