Python稀疏矩阵的转置
- '''
- 稀疏矩阵的转置
- 输入: 一个稀疏矩阵
- 输出: 矩阵的转置
- 方法: 中间可以利用三元组进行操作
- '''
- from numpy import *
- def sparseToTriple(matrix):
- m, n = shape(matrix)
- triple = []
- for i in range(m):
- for j in range(n):
- if matrix[i][j] != 0:
- triple.append([i, j, matrix[i][j]])
- return triple
-
- def transTriple(triple):
- m, n = shape(triple)
- transMatrix = []
- sortedIndex = array([m[1] for m in triple]).argsort()
- for i in range(m):
- tempArray = triple[sortedIndex[i]]
- transMatrix.append([tempArray[1], tempArray[0], tempArray[2]])
- return transMatrix
-
- def tripleToSparse(triple, m, n):
- outMatrix = zeros([n, m])
- for pointTuple in triple:
- mLocation = pointTuple[0]
- nLocation = pointTuple[1]
- value = pointTuple[2]
- outMatrix[mLocation][nLocation] = value
- return outMatrix
-
- def matrixTrans(matrix):
- m, n = shape(matrix)
- triple = sparseToTriple(matrix)
- transedTriple = transTriple(triple)
- transedMatrix = tripleToSparse(transedTriple, m, n)
- return transedMatrix
-
- matrix = [[0, 0, 0, 0, 0, 0, 6, 0, 0],
- [0, 0, 2, 0, 0, 1, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0],
- [3, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 5, 0],
- [0, 7, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 8],
- [0, 0, 5, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 2, 0, 0, 0, 0, 0]]
-
- transedMatrix = matrixTrans(matrix)
- print(transedMatrix)
解压密码: detechn或detechn.com
免责声明
本站所有资源出自互联网收集整理,本站不参与制作,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站发布资源来源于互联网,可能存在水印或者引流等信息,请用户自行鉴别,做一个有主见和判断力的用户。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。