在Python中,将矩阵的列乘以不同的数字

2024-09-30 14:37:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本中的矩阵hsa。现在我想用numpy将它的第一列乘以sqrt(2),第二列乘以sqrt(2),第三列乘以1来创建一个结果矩阵HSA(预期)?我使用了给定的代码,但它不起作用

import pandas as pd import numpy as np import math hs = np.loadtxt('HS', skiprows=8) from itertools import islice with open('HS') as lines: array = np.genfromtxt(islice(lines, 2, 5)) lv=np.diag(array) hsa=hs*lv print (hsa) HSA= [sqrt(2)*hsa(:,1), sqrt(2)*hsa(:,2), 1*hsa(:,1)] print (HSA) hsa= [[1 0 5] [1 0 7] [1 0 9] [1 0 5] [1 0 6] [1 0 4] [1 0 5] [0 1 6] [0 1 1] [0 1 7] [0 1 3] [0 1 3] [0 1 5]] (Expected) HSA= [[1.414 0 5] [1.414 0 7] [1.414 0 9] [1.414 0 5] [1.414 0 6] [1.414 0 4] [1.414 0 5] [0 1.414 6] [0 1.414 1] [0 1.414 7] [0 1.414 3] [0 1.414 3] [0 1.414 5]]

Tags: importnumpyasnp矩阵sqrtarrayhs