Skip to content Skip to sidebar Skip to footer

How To Vectorize The Creation Of N Rotation Matrix From A Vector Of Angles?

Data: theta is a vector of N angles. Question: How to create a vector of N 2D rotation matrix from theta while vectorizing? Without vectorizing I can think of a for loop: import nu

Solution 1:

You can vectorize your function by using the vectorized versions of cos and sin, then rearranging the result:

defR_vec(theta):
    c, s = np.cos(theta), np.sin(theta)
    return np.array([c, -s, s, c]).T.reshape(len(theta),2,2)

For N=100, the vectorized versions is about 110 times faster than the original on my computer.

Post a Comment for "How To Vectorize The Creation Of N Rotation Matrix From A Vector Of Angles?"