【问题标题】:How to add and subtract the matrix index values in a function?如何在函数中添加和减去矩阵索引值?
【发布时间】:2022-11-04 21:52:59
【问题描述】:

假设我有一个 3x3 矩阵。在一个函数中,我想定义两个变量 m , n 作为矩阵维度的索引号。

我想减去所有可能的组合。我将如何在函数中定义它?

即:(m-n)应该做=(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 

【问题讨论】:

  • 期望的输出是什么?*

标签: python pandas numpy matrix


【解决方案1】:

不确定清楚地理解这个问题,但这里是一个尝试:

您可以为 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

希望这可以帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多