我正在尝试从CSV文件读取一些值的同时,对一些数组进行一些数值运算。
我有一个固定的接收器坐标,并且我从跟踪太阳的CSV文件中读取了定日镜的坐标。
接收者的坐标:
# co-ordinates of Receiver
XT = 0 # X co-ordinate of Receiver
YT = 0 # Y co-ordinate of Receiver
ZT = 207.724 # Z co-ordinate of Receiver, this is the height of tower
A = np.array(([XT],[YT],[ZT]))
print(A," are the co-ordinates of the target i.e. the receiver")
十个定日镜的坐标:我从CSV文件中读取了以下数据以及以下数据:
#X,Y,Z
#-1269.56,-1359.2,5.7
#1521.28,-68.0507,5.7
#-13.6163,1220.79,5.7
#-1388.76,547.708,5.7
#1551.75,-82.2342,5.7
#405.92,-1853.83,5.7
#1473.43,-881.703,5.7
#1291.73,478.988,5.7
#539.027,1095.43,5.7
#-1648.13,-73.7251,5.7
我读取了CSV的坐标,如下所示:
import csv
# Reading data from csv file
with open('Heliostat Field Layout Large heliostat.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
X = []
Y = []
Z = []
for row in readCSV:
X_coordinates = row[0]
Y_coordinates = row[1]
Z_coordinates = row[2]
X.append(X_coordinates)
Y.append(Y_coordinates)
Z.append(Z_coordinates)
Xcoordinate = [float(X[c]) for c in range(1,len(X))]
Ycoordinate=[float(Y[c]) for c in range(1,len(Y))]
Zcoordinate=[float(Z[c]) for c in range(1,len(Z))]
现在,当我尝试打印十个定日镜的坐标时,我得到了三个大数组,其中所有Xcoordinate,Ycoordinate和Zcoordinate分组为一个,而不是十个不同的输出。
[[[-1269.56 1521.28 -13.6163 -1388.76 1551.75 405.92 1473.43
1291.73 539.027 -1648.13 ]]
[[-1359.2 -68.0507 1220.79 547.708 -82.2342 -1853.83
-881.703 478.988 1095.43 -73.7251]]
[[ 5.7 5.7 5.7 5.7 5.7 5.7 5.7
5.7 5.7 5.7 ]]] are the co-ordinates of the heliostats
我用了:
B = np.array(([Xcoordinate],[Ycoordinate],[Zcoordinate]))
print(B," are the co-ordinates of the heliostats")
怎么了
此外,我想有一个数组,我像B-A那样使用:
#T1 = matrix(A)- matrix(B)
#print(T1," is the target vector for heliostat 1, T1")
我应该如何对数组A和B进行数值运算?我在这里尝试了矩阵运算。错了吗
您的密码是正确的
以下输出是显示numpy数组的方式。
[[-1359.2 -68.0507 1220.79 547.708 -82.2342 -1853.83 -881.703 478.988 1095.43 -73.7251]]
尽管存在将值卡在一起的错觉,但它们在数组中完全不同。您可以使用访问单个值
print(B[1, 0, 0]) # print Y[0]
您要执行的数组A和B的减法将起作用
T1 = np.matrix(A)- np.matrix(B)
print(T1," is the target vector for heliostat 1, T1")
我可以提出两个建议吗?
您可以使用loadtxt函数numpy读取文本文件中的矩阵形式的numpy数组:
your_file = 'Heliostat Field Layout Large heliostat.csv'
B = np.loadtxt(your_file, delimiter=',', skiprows=1)
结果将是(3,10)的numpy数组。
您可以直接在numpy数组上执行宽范围操作(因此您无需将其转换为矩阵)。您只需要注意尺寸。
在原始脚本中,您只需要编写:
T1 = A - B
如果按照建议将数组B与loadtxt结合使用,则将获得(10,3)数组,而A是(3,1)数组。数组B必须首先以(3,10)数组进行整形:
B = B.reshape((3, 10))
T1 = A - B
编辑:计算T1的每个3D向量的范数
norm_T1 = np.sqrt( np.sum( np.array(T1)**2, axis=0 ) )
请注意,在您的代码中T1是矩阵,因此T1 ** 2是矩阵乘积。为了计算T1的每个向量v的sqrt(v [0] ** 2 + v [1] ** 2 + v [2] ** 2),我首先将其转换为numpy数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句