Ku-trala
Ku-trala

Reputation: 651

Building a matrix

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.

Upvotes: 1

Views: 113

Answers (1)

Beasterfield
Beasterfield

Reputation: 7123

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...)

Upvotes: 2

Related Questions