當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Numpy matrix.setfield()用法及代碼示例

借助matrix.setfield()方法中,矩陣中的所有字段均設置為以參數形式傳遞的值。

用法: matrix.setfield()

返回:將值作為參數傳遞的新矩陣


範例1:

在這個例子中,我們可以看到matrix.setfield()方法用於生成具有值的新矩陣,該值在此方法中作為參數傳遞。

# import the important module in python 
import numpy as np 
            
# make matrix with numpy 
gfg = np.matrix('[4, 1; 12, 3]') 
            
# applying matrix.setfield() method 
gfg.setfield(2, np.int32) 
  
print(gfg)
輸出:
[[2 2]
 [2 2]]


範例2:

# import the important module in python 
import numpy as np 
            
# make matrix with numpy 
gfg = np.matrix('[4, 1, 9; 12, 3, 1; 4, 5, 6]') 
            
# applying matrix.setfield() method 
gfg.setfield(25, np.int32) 
  
print(gfg)
輸出:
[[25 25 25]
 [25 25 25]
 [25 25 25]]


相關用法


注:本文由純淨天空篩選整理自Jitender_1998大神的英文原創作品 Python | Numpy matrix.setfield()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。