简体   繁体   English

通过相同的方程式处理不同的数据

[英]Processing different data through same equation

I have an array that looks like this: 我有一个看起来像这样的数组:

A=[  id.                 r            d    ]
[[  47.          223.25190261   58.0551391 ]
 [  49.          223.25102751   58.05662719]
 [  57.          223.25013049   58.05139459]]

The first column isnt important. 第一列并不重要。 The following two are though, they are coordinates. 尽管以下两个是坐标。

I have to compare EACH set of coordinates (column 2 and 3 together) against these coordinates: (223.251, 58.05) by using this equation: B=sin(D)sin(d) + cos(D)cos(d)cos(Rr). 我必须使用以下等式将每个坐标集(第2列和第3列)与此坐标进行比较:(223.251,58.05):B = sin(D)sin(d)+ cos(D)cos(d)cos( RR)。

Where (R,D) are the original coordinates (223.251, 58.05) and (r,d) are the coordinates in the array. 其中(R,D)是原始坐标(223.251,58.05),而(r,d)是数组中的坐标。 How do I do this for each set of coordinates in the array without having to input the numbers myself or having to define each number and replace them with the next set of coordinates? 如何为数组中的每个坐标集执行此操作,而不必自己输入数字或必须定义每个数字并将其替换为下一组坐标? I want the program to obviously keep (R,D) consistent and change (r,d) for each row and make the calculations. 我希望程序明显地保持(R,D)一致并为每一行更改(r,d)并进行计算。 After it's done making the calculation for each row I want to have them output. 完成每一行的计算后,我想让它们输出。 I really have no idea how to do this, I'm thinking maybe something with a loop. 我真的不知道该怎么做,我在想也许有些循环。 I'm seriously lost. 我很迷路。 The end of the code is this: 代码的结尾是这样的:

B=(((sin(dec0))*(sin(dec1)) + (cos(dec0)*cos(dec1))*(cos(ra0-ra1))))
print B
0.540302302454

But this only does the first row of coordinates, I want it to be done manually 但这只是第一行坐标,我希望手动完成

I'm not sure if the formula is correct and data representative, because your values are really close to each other. 我不确定公式是否正确并且可以表示数据,因为您的值之间确实很接近。 Anyway, to print the B value for each item in your data, you can use: 无论如何,要打印数据中每个项目的B值,可以使用:

from math import radians, sin, cos

orig = (223.251, 58.05)
R = radians(orig[0])
D = radians(orig[1])

A = [[  47.,  223.25190261,   58.0551391 ],
     [  49.,  223.25102751,   58.05662719],
     [  57.,  223.25013049,   58.05139459]]

for item in A:
  r = radians(item[1])
  d = radians(item[2])
  B = sin(D)*sin(d) + cos(D)*cos(d)*cos(R-r)
  print(B)

if you've got numpy array as input, use numpy module instead of math of course. 如果您有numpy数组作为输入,那么当然可以使用numpy模块而不是math

If you are willing to use NumPy you the operations can be vectorized avoiding the for loops like: 如果您愿意使用NumPy,则可以对操作进行矢量化处理,避免使用for循环:

from numpy import array, deg2rad, sin, cos

orig = (223.251, 58.05)
R = deg2rad(orig[0])
D = deg2rad(orig[1])

A = array([[47., 223.25190261, 58.0551391 ],
           [49., 223.25102751, 58.05662719],
           [57., 223.25013049, 58.05139459]])

r = deg2rad(A[:,1])
d = deg2rad(A[:,2])
B = sin(D)*sin(d) + cos(D)*cos(d)*cos(R-r)

where B is a numpy.ndarray containing the result for each line of A . 其中B是一个numpy.ndarray包含A每一行的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM