Reputation: 97
I have two lists: Dir and Vel. Each item in each list is a float (list of floats). So 1500 elements in Vir (a list), whereby each element is a float. Same for Dir. Values are both NaNs and then real numbers like 1, 1.5, 2, 2.5, etc. I am trying to compute u = Vel * sin(radians(Dir)). In Matlab it would be simply Vel .* sin(radians(Dir)).
Dir and Vel are both type list. When I open the list, there are 1500 elements of type float. I have tried float(Dir) without any luck. I have math and numpy installed. My specific code is:
u = [a * b for a, b in zip(Vel,sin(radians(Dir)))]
I am trying to compute the u and v vector shown above but I keep getting the error that a float is required - even though each list contains only floats. What am I doing wrong?
Upvotes: 0
Views: 1053
Reputation: 7920
You are probably trying to apply the standard math.sin
and math.radians
to an iterable, which is not going to work (they require a scalar). Luckily, NumPy supports vector operations:
import numpy as np
vel = np.array([3., 10., 40.])
dir = np.array([30., 0., 90.])
u = vel * np.sin(np.radians(dir)) # element-wise multiplication
print(u) # prints [ 1.5 0. 40. ]
Upvotes: 2
Reputation: 1508
The problem is that you are doing sin(radians(Dir))
when Dir
is a list. Try doing the math on a
and b
instead.
[v*(sin(radians(d))) for v,d in zip(vel, dir)]
Upvotes: 1