In this article, we will discuss how to convert array elements consisting of different angles as radian to degree.
To convert an angle from radians to degrees we need to simply multiply the radians by 180°/Π?. We can also use the numpy.rad2deg() method to perform the same operation.
Now suppose we have an array containing angles in radian now our task is to convert that array into elements containing element in degree.
Example :
Input : [Π, Π/4, Π/2, 3Π/2, 2Π) Output : [180. 45. 90. 270. 360.]
Now let us try to implement this using Python:
Example 1 : We will be using the numpy.pi attribute for Π.
# importing the module import numpy as np # creating an array containing angles in radians rad = np.array([np.pi, np.pi / 4 , np.pi / 2 , 3 * np.pi / 2 , 2 * np.pi]) print ( "The angles in radian" ) print (rad) # converting array from radian to degree deg = np.rad2deg(rad) print ( "\nThe angles in degree" ) print (deg) |
Output :
The angles in radian [3.14159265 0.78539816 1.57079633 4.71238898 6.28318531] The angles in degree [180. 45. 90. 270. 360.]
Example 2 : This time the conversion will be performed without using the inbuilt method.
# importing the module import numpy as np # creating an array containing angles in radians rad = np.array([ 3.14159265 , 0.78539816 , 1.57079633 , 4.71238898 , 6.28318531 ]) print ( "The angles in radian" ) print (rad) # converting array from radian to degree deg = [] for angle in rad: deg.append( round (angle * ( 180 / np.pi))) print ( "\nThe angles in degree" ) print (deg) |
Output :
The angles in radian [3.14159265 0.78539816 1.57079633 4.71238898 6.28318531] The angles in degree [180.0, 45.0, 90.0, 270.0, 360.0]
<!–
–>
Please Login to comment…