当前位置:网站首页>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.
边栏推荐
- Too many requests at once, and the database is in danger
- Synchronized与锁升级
- CCID Consulting released the database Market Research Report on key application fields during the "14th five year plan" (attached with download)
- 【mysql进阶】MTS主从同步原理及实操指南(七)
- 阅读别人的代码,是一种怎样的体验
- SFINAE
- Step by step expansion of variable parameters in class templates
- 每日3题(1):找到最近的有相同 X 或 Y 坐标的点
- Strong, weak, soft and virtual references of ThreadLocal
- Openssf security plan: SBOM will drive software supply chain security
猜你喜欢

Semaphore of thread synchronization

【业务安全-04】万能用户名及万能密码实验

Redis persistence

Web chat room system based on SSM

Practice of constructing ten billion relationship knowledge map based on Nebula graph

How to select cross-border e-commerce multi merchant system

Buuctf Misc

SQL parsing practice of Pisa proxy

Référence forte, faible, douce et virtuelle de threadlocal

Pytoch learning 2 (CNN)
随机推荐
enable_ if
海量数据!秒级分析!Flink+Doris构建实时数仓方案
CCID Consulting released the database Market Research Report on key application fields during the "14th five year plan" (attached with download)
Synchronized与锁升级
Bidding announcement: Oracle database maintenance service procurement of the First Affiliated Hospital of Jinan University
【高等数学】从法向量到第二类曲面积分
基于 Nebula Graph 构建百亿关系知识图谱实践
跨境电商多商户系统怎么选
重读经典:《The Craft of Research(1)》
[PHP code injection] common injectable functions of PHP language and utilization examples of PHP code injection vulnerabilities
Kyndryl partnered with Oracle and Veritas
Pychart installation and setup
Redis persistence
Reflection learning summary
优雅的自定义 ThreadPoolExecutor 线程池
基于Vue+Node+MySQL的美食菜谱食材网站设计与实现
线程同步之信号量
American chips are hit hard again, and another chip enterprise after Intel will be overtaken by Chinese chips
解析Activity启动-生命周期角度
隱私計算FATE-離線預測