在 Numpy 中將二維陣列的陣列元素的位右移


要將二維陣列的陣列元素的位右移,請在 Python Numpy 中使用 **numpy.right_shift()** 方法。位右移 x2 次。因為數字的內部表示形式為二進位制格式,所以此操作等效於將 x1 除以 2**x2。

x1 是輸入值。x2 是要從 x1 右側移除的位數。如果 x1.shape != x2.shape,則它們必須可廣播到一個公共形狀。

函式 right_shift() 返回 x1,其位右移 x2 次。如果 x1 和 x2 都是標量,則這是一個標量。

步驟

首先,匯入所需的庫 -

import numpy as np

建立一個二維陣列 -

arr = np.array([[56, 87, 23], [92, 81, 98]])

顯示我們的陣列 -

print("Array...
",arr)

獲取資料型別 -

print("
Array datatype...
",arr.dtype)

獲取陣列的維度 -

print("
Array Dimensions...
",arr.ndim)

獲取陣列的形狀 -

print("
Our Array Shape...
",arr.shape)

獲取陣列的元素個數 -

print("
Elements in the Array...
",arr.size)

右移的計數 -

valRight = 3

要將二維陣列的陣列元素的位右移,請使用 numpy.right_shift() 方法 -

print("
Result (right shift)...
",np.right_shift(arr, valRight))

示例

import numpy as np

# Create a Two-Dimensional array
arr = np.array([[56, 87, 23], [92, 81, 98]])

# Displaying our array
print("Array...
",arr) # Get the datatype print("
Array datatype...
",arr.dtype) # Get the dimensions of the Array print("
Array Dimensions...
",arr.ndim) # Get the shape of the Array print("
Our Array Shape...
",arr.shape) # Get the number of elements of the Array print("
Elements in the Array...
",arr.size) # The count of right right valRight = 3 # To shift the bits of an integer to the right, use the numpy.right_shift() method in Python Numpy print("
Result (right shift)...
",np.right_shift(arr, valRight))

輸出

Array...
[[56 87 23]
[92 81 98]]

Array datatype...
int64

Array Dimensions...
2

Our Array Shape...
(2, 3)

Elements in the Array...
6

Result (right shift)...
[[ 7 10 2]
[11 10 12]]

更新於: 2022-02-22

356 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.