Reputation: 483
I need a help to update my theta value for each last two numbers for 5 element in the message:
msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
def get_new_theta(msg):
# addition for each last 2 number in 5 elements in the msg
# for example: new_theta=theta+(4+5), new_theta=new_theta+(9+10) and so on...
return new_theta
#initial theta
theta=1
for b in msg:
hwpx = [0,math.cos(4*math.radians(theta)),math.sin(4*math.radians(theta)), 0]
a=b*hwpx
print (a)
This is the expected output:
theta=1
1*[0,math.cos(4*math.radians(1)),math.sin(4*math.radians(1)), 0]
2*same as above
3*same as above
4*same as above
5*same as above
theta=1+(4+5)=10
6*[0,math.cos(4*math.radians(10)),math.sin(4*math.radians(10)), 0]
7*same as above
8*same as above
9*same as above
10*same as above
theta=10+(9+10)=29
Noted that the value of theta will be update for each 5 element. And the new theta will be used to calculated for the next element.
However,when I run this code, loop was not successfully implemented.
msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
theta=1
def get_new_theta(msg, theta):
new_theta = [theta]
for a, b in zip(msg[3::5], msg[4::5]):
new_theta.append(new_theta[-1] + a + b)
return new_theta
theta=1
for b in msg:
theta=get_new_theta(msg, theta)
hwpx = [0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
a=b*hwpx
print (theta)
I got this error:
hwpx = [0, math.cos(4*math.radians(theta)), math.sin(4*math.radians(theta)), 0]
TypeError: a float is required`
Thank you
Upvotes: 1
Views: 134
Reputation: 555
try
def get_new_theta(msg, theta):
new_theta = [theta]
for a, b in zip(msg[3::5], msg[4::5]):
new_theta.append(new_theta[-1] + a + b)
return new_theta
or if you prefer
def get_new_theta(msg, theta):
yield theta
for a, b in zip(msg[3::5], msg[4::5]):
theta += a + b
yield theta
Edit
It feels a bit weird that my answer got accepted but your problem is not truly solved yet...
Now the problem is that you've asked about theta, but didn't tell us what you truly want. What is hwpx? Is it supposed to be a list? For now, I can only provide something like
# get_new_theta() returns [1, 10, 29, 58]
# but I don't know what to do with the last 58 so slice it out
for i, theta in enumerate(get_new_theta(msg, 1)[:-1]):
for j in range(5):
print(theta, msg[i*5+j])
# you may want to do msg[i*5+j] * f(theta) here.
1 1
1 2
1 3
1 4
1 5
10 6
10 7
10 8
10 9
10 10
29 11
29 12
29 13
29 14
29 15
Upvotes: 2
Reputation: 106465
Try:
msg = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
def get_new_theta(msg, theta):
return theta + sum(msg[3::5]) + sum(msg[4::5])
theta=1
print(get_new_theta(msg, theta))
This outputs: 58
(1 + 4 + 5 + 9 + 10 + 14 + 15 = 58)
Upvotes: 0
Reputation: 1924
Does this do what you want?
def get_new_theta(msg, theta):
new_theta = theta + msg[-2] +msg[-1]
return new_theta
Upvotes: 0