numpy.matrix.getfield#
方法
- matrix.getfield(dtype, offset=0)#
以特定型態傳回給定陣列的欄位。
欄位是具有給定資料型態的陣列資料視圖。視圖中的值由給定的型態和當前陣列的位元組偏移量決定。偏移量需要確保視圖 dtype 符合陣列 dtype;例如,dtype complex128 的陣列具有 16 位元組的元素。如果使用 32 位元整數(4 位元組)取得視圖,則偏移量需要在 0 到 12 位元組之間。
- 參數:
- dtypestr 或 dtype
視圖的資料型態。視圖的 dtype 大小不能大於陣列本身的大小。
- offsetint
開始元素視圖前要跳過的位元組數。
範例
>>> import numpy as np >>> x = np.diag([1.+1.j]*2) >>> x[1, 1] = 2 + 4.j >>> x array([[1.+1.j, 0.+0.j], [0.+0.j, 2.+4.j]]) >>> x.getfield(np.float64) array([[1., 0.], [0., 2.]])
透過選擇 8 位元組的偏移量,我們可以為視圖選擇陣列的複數部分
>>> x.getfield(np.float64, offset=8) array([[1., 0.], [0., 4.]])