How do I convert a n*1 matrix to a n*n diagonal matrix

后端 未结 3 1130
梦如初夏
梦如初夏 2021-01-19 11:04

I have a nx1 matrix I want to convert this to a nxn diagonal matrix in R

\"matrix

3条回答
  •  佛祖请我去吃肉
    2021-01-19 11:29

    As @Ben Bolker has suggested, you can simply define your identity matrix using diag:

    my.matrix <- diag(0.25, 4)
    my.matrix
    ##      [,1] [,2] [,3] [,4]
    ## [1,] 0.25 0.00 0.00 0.00
    ## [2,] 0.00 0.25 0.00 0.00
    ## [3,] 0.00 0.00 0.25 0.00
    ## [4,] 0.00 0.00 0.00 0.25
    

提交回复
热议问题