0

I would like to do nested loop in my programming. However, this code did not meet my expectation.

X=[0,1,1,1,0]

length=len(X)
for i,val in enumerate(X):
    a=0
    count=0
    while (count<length):
        a=15+a
        print (a)
        HWPQ=np.matrix([[1, 0, 0, 0], [0, math.cos(4*math.radians(a)), 
        math.sin(4*math.radians(a)), 0], [0, math.sin(4 * math.radians(a)), - 
        math.cos(4 * math.radians(a)), 0], [0, 0, 0, -1]])
        result=HWPQ*val
        print (result)
        count=count +1
    print ("\n")

Supposedly, in this program, I would like to update the value of a for each element using the loop. For example:

X=0 for a=0
X=1 for a=15
X=1 for a=30
X=1 for a=45
X=0 for a=60

The result should be, as I calculated it manually:

[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
[[ 1.         0.         0.         0.       ]
 [ 0.         0.5        0.8660254  0.       ]
 [ 0.         0.8660254 -0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]
[[ 1.         0.         0.         0.       ]
 [ 0.        -0.5        0.8660254  0.       ]
 [ 0.         0.8660254  0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]
[[  1.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00  -1.00000000e+00   1.22464680e-16   0.00000000e+00]
 [  0.00000000e+00   1.22464680e-16   1.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00  -1.00000000e+00]]
 [[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
5
  • What's X used in this code? Seems useless? a just a arithmetic progression, ignore any value of X Commented Jul 27, 2018 at 3:11
  • Is that last one correct? you're multiplying your matrix by val, which is 0, so the whole matrix would be 0s. Commented Jul 27, 2018 at 3:11
  • @ i alarmed alien. You are correct. I have updated the last output. Commented Jul 27, 2018 at 3:14
  • @atline. The value of X is represented as list of bit 0 or 1. Commented Jul 27, 2018 at 3:16
  • Yes, I missed it, it's ok. Commented Jul 27, 2018 at 3:17

1 Answer 1

1

There seem to be a load of unnecessary variables in your code, like count, X, and i. Removing them and using a standard for loop, you get:

X=[0,1,1,1,0]
a=0
for val in X:
    print (a)
    HWPQ=np.matrix([
        [1, 0, 0, 0], 
        [0, math.cos(4*math.radians(a)), math.sin(4*math.radians(a)), 0], 
        [0, math.sin(4 * math.radians(a)), - math.cos(4 * math.radians(a)), 0],
        [0, 0, 0, -1]])
    result=HWPQ*val
    print (result)
    a=15+a
    print ("\n")

which creates the output that you want.

0
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]


15
[[ 1.         0.         0.         0.       ]
 [ 0.         0.5        0.8660254  0.       ]
 [ 0.         0.8660254 -0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]


30
[[ 1.         0.         0.         0.       ]
 [ 0.        -0.5        0.8660254  0.       ]
 [ 0.         0.8660254  0.5        0.       ]
 [ 0.         0.         0.        -1.       ]]


45
[[  1.00000000e+00   0.00000000e+00   0.00000000e+00   0.00000000e+00]
 [  0.00000000e+00  -1.00000000e+00   1.22464680e-16   0.00000000e+00]
 [  0.00000000e+00   1.22464680e-16   1.00000000e+00   0.00000000e+00]
 [  0.00000000e+00   0.00000000e+00   0.00000000e+00  -1.00000000e+00]]


60
[[ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]
 [ 0.  0.  0.  0.]]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.