假设我有一个3x3矩阵。在一个函数中,我想定义两个变量m,n作为矩阵维数的指数。
我想减去所有可能的组合。我如何在函数中定义它呢?
即:(M)应该做= (1-1),(1-2),(1-3),(2-1),(2-2),(2-3),(3-1),(3,2),(3,3)。不是矩阵单元格值,而是索引值(1,2,3)。
df_matrix是多指标熊猫数据采集系统。
df_matrix
m 1 2 3
n
1 x y z
2 a b c
3 p q r
发布于 2022-11-04 13:52:59
更简单的是,没有循环,您可以这样做(M是can矩阵):
m = np.arange(M.shape[0]) #List of indexes of the first dimension (lines)
m = m.reshape((M.shape[0], 1)) #transpose to obtain a vertical matrix
n=np.arange(M.shape[1]) #List of indexes of second dimensions (columns)
S = np.sum(M * (m-n)**2) #Compute the sum
*下面的第一个答案,在我看来并不是那么漂亮.***
不一定要清楚地理解这个问题,但这里有一个尝试:
您可以为m和n的所有可能值创建一个嵌套循环。
# Loop over all indexes of the first dimension
for m in range(df_matrix.shape[0]):
# Loop over all indexes of the second dimension
for n in range(df_matrix.shape[1]):
# Do whatever you want with indexes
diff = m - n
希望这能有所帮助!
https://stackoverflow.com/questions/74317992
复制相似问题