0

I have a list of a total of 55 numeric values. I want to create a 10x10 matrix in which only the lower (or upper) triangular matrix (with the diagonal itself) is populated. I know that I can use lower.tri() to create a lower triangular matrix, however, when I use this function, it seems like data is not populated by row. If i use, matrix(v, nrow= 10, ncol= 10, byrow= TRUE) then the full matrix is populated instead of just the lower diagonal. I have seen solutions to a similar problem (Fill lower matrix with vector by row, not column), but in that example, they use only 6 variables, whereas I have 10, and that solution gets distorted for me.

v <- 1:55
m <- diag(10)
Darren Tsai
  • 32,117
  • 5
  • 21
  • 51

1 Answers1

0

A simple trick can do. For example, to populate a lower triangular matrix by row. First, populate an upper triangular matrix by column(which is by default), then transform the matrix. And vice versa.
Because R fill matrix in column by default, just fill the transformed matrix first, and transform it back.
Code example
m = diag(10)
upperm = upper.tri(m, diag = T)
m[upperm] = v; t(m)

Kate
  • 1
  • So how is it different from the answer the OP has linked in his question? – David Arenburg Feb 07 '19 at 19:37
  • @DavidArenburg They are the same. From the question, it looks like the explicit codes for 10 variables and some explanations are needed to help understand the logic ... – Kate Feb 07 '19 at 21:07