当前位置:网站首页>R language triple becomes matrix matrix becomes triple
R language triple becomes matrix matrix becomes triple
2022-06-27 14:31:00 【Analysis of breeding data】
1. Generate a matrix
- Use
matrix
function , Generate a 4*4 Matrix
> # Generate a matrix
> mat = matrix(1:16,4,4)
> mat
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
2. Change the matrix into a symmetric matrix according to the lower triangle – Method 1
- Use here
upper.tri
function - Use here
t()
Transposed
> # According to the lower triangle , Make it a symmetric matrix -- Method 1
> mat1 = mat
> mat1[upper.tri(mat1)] <- t(mat1)[upper.tri(mat1)]
> mat1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 6 7 8
[3,] 3 7 11 12
[4,] 4 8 12 16
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
3. Change the matrix into a symmetric matrix according to the lower triangle – Method 2
> # According to the lower triangle , Make it a symmetric matrix -- Method 2
> mat2 = mat
> mat2[upper.tri(mat2)] = 0
> mat2 = mat2+t(mat2)
> diag(mat2) = diag(mat2)/2
> mat2
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 6 7 8
[3,] 3 7 11 12
[4,] 4 8 12 16
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
> # The symmetric matrix becomes a triple
> mat1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 6 7 8
[3,] 3 7 11 12
[4,] 4 8 12 16
> library(learnasreml)
> rc1 = write_relation_matrix(mat1,"none")
> rc1
Row Column coeff
1 1 1 1
2 2 1 2
6 2 2 6
3 3 1 3
7 3 2 7
11 3 3 11
4 4 1 4
8 4 2 8
12 4 3 12
16 4 4 16
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
> # Triple to matrix
> rc1
Row Column coeff
1 1 1 1
2 2 1 2
6 2 2 6
3 3 1 3
7 3 2 7
11 3 3 11
4 4 1 4
8 4 2 8
12 4 3 12
16 4 4 16
>
> re_mat = matrix(0,max(rc1$Row),max(rc$Column))
> re_mat
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 0 0 0 0
[3,] 0 0 0 0
[4,] 0 0 0 0
> re_mat[as.matrix(rc[,1:2])]=rc[,3]
> re_mat
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 2 6 0 0
[3,] 3 7 11 0
[4,] 4 8 12 16
> re_mat[upper.tri(re_mat)] = t(re_mat)[upper.tri(re_mat)]
> re_mat
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 2 6 7 8
[3,] 3 7 11 12
[4,] 4 8 12 16
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
边栏推荐
猜你喜欢
Naacl 2022 | TAMT: search the transportable Bert subnet through downstream task independent mask training
Julia1.1 installation instructions
【微服务|Sentinel】热点规则|授权规则|集群流控|机器列表
The global chip market may stagnate, and China's chip expansion accelerates to improve its self-sufficiency rate against the trend
原子操作类
[business security 03] password retrieval business security and interface parameter account modification examples (based on the metinfov4.0 platform)
What if the win system cannot complete the update and is revoking the status change
Leetcode 724. 寻找数组的中心下标(可以,一次过)
Acwing game 57
[PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities
随机推荐
What kind of experience is it to read other people's code
Kyndryl partnered with Oracle and Veritas
enable_if
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
Design and implementation of food recipe and ingredients website based on vue+node+mysql
Reflection learning summary
How to set the compatibility mode of 360 speed browser
【mysql进阶】MTS主从同步原理及实操指南(七)
Leetcode 724. 寻找数组的中心下标(可以,一次过)
招标公告:暨南大学附属第一医院Oracle数据库维保服务采购
June 27, 2022 Daily: swin transformer, Vit authors and others said: a good basic model is the simple pursuit of CV researchers
剑指 Offer II 039. 直方图最大矩形面积 单调栈
为什么 Oracle 云客户必须在Oracle Cloud 季度更新发布后自行测试?
NLP - monocleaner
AcWing 第57 场周赛
线程同步之信号量
Design and implementation of reading app based on Web Platform
What if the win system cannot complete the update and is revoking the status change
The global chip market may stagnate, and China's chip expansion accelerates to improve its self-sufficiency rate against the trend
Bidding announcement: Oracle database maintenance service procurement of the First Affiliated Hospital of Jinan University