使用Python deg2rad()函式將角度從度轉換為弧度


要將度陣列轉換為弧度,請在 Python Numpy 中使用 numpy.deg2rad() 方法。該方法返回相應的弧度角。如果 x 是標量,則這是一個標量。第一個引數是以度為單位的輸入角。第二個和第三個引數是可選的。

第二個引數是 ndarray,結果儲存到的位置。如果提供,則其形狀必須與輸入廣播到的形狀相同。如果沒有提供或為 None,則返回一個新分配的陣列。

第三個引數是對輸入進行廣播的條件。在條件為 True 的位置,out 陣列將設定為 ufunc 結果。在其他位置,out 陣列將保留其原始值。

步驟

首先,匯入所需的庫:

import numpy as np

建立陣列:

arr = np.arange(12.)

顯示我們的陣列:

print("Array...\n",arr)

獲取資料型別:

print("\nArray datatype...\n",arr.dtype)

獲取陣列的維度:

print("\nArray Dimensions...\n",arr.ndim)

獲取陣列的元素個數:

print("\nNumber of elements in the Array...\n",arr.size)

度陣列:

res = arr*30

要將度陣列轉換為弧度,請在 Python Numpy 中使用 numpy.deg2rad() 方法。該方法返回相應的弧度角。如果 x 是標量,則這是一個標量:

print("\nDegree array to degrees...\n",np.deg2rad(res))

示例

import numpy as np

# Create an Array
arr = np.arange(12.)

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimensions...\n",arr.ndim)

# Get the number of elements in the Array
print("\nNumber of elements...\n", arr.size)

# Degree array
res = arr*30

# To convert a degree array to radians, use the numpy.deg2rad() method in Python Numpy
# The method returns the corresponding angle in radians. This is a scalar if x is a scalar.
print("\nDegree array to degrees...\n",np.deg2rad(res))

輸出

Array...
[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11.]

Our Array type...
float64

Our Array Dimensions...
1

Number of elements...
12

Degree array to degrees...
[0. 0.52359878 1.04719755 1.57079633 2.0943951 2.61799388
3.14159265 3.66519143 4.1887902 4.71238898 5.23598776 5.75958653]

更新於:2022年2月28日

2K+ 次檢視

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.