【发布时间】:2016-01-18 19:30:03
【问题描述】:
我有一个包含 4950 值的文件,例如:
0.012345678912345678
我使用以下方式读取文件:
a = numpy.genfromtxt(file_name, dtype=str, delimiter=',') # a.shape = (4950L, 1L) #dtype=str as I don't want to compromise accuracy
#say a == ['0.000000000000000001', -'0.000000000000000002', ...., '0.000000000004950']
我想要实现的是获得一个大小为(100L, 100L) 的矩阵b,其:
- 上三角值用 numpy 数组 'a' 中的值填充。
- 下三角值用 numpy 数组“a”中的值填充,但乘以 -1。
- 对角线仅包含零。
示例(准确性很重要):
array = ['1','2','-3','-5','6','-7'] # In reality the data is up to 18 decimal places.
final_matrix = [
['0','1','2','-3'],
['-1',0,'-5','6'],
['-2','5','0','-7'],
['3','-6','7','0']
]
实现这一目标的最有效方法是什么?
【问题讨论】:
-
你的文件格式是什么?一行、一列、多列?
-
stackoverflow.com/questions/34234965/… - 最近的一个类似问题
Copy flat list of upper triangle entries to full matrix。