n维矩阵的递归

时间:2019-07-02 12:21:36

标签: python matrix

我正在用Python 3做一些实现,我需要一些帮助。我有函数,这取决于矩阵的维数。我实际上有两个矩阵,并希望以某种方式获得第三个矩阵。例如(二维矩阵):

def my_matrix_2d(mat1, mat2):
    ar = []
    for i in range(len(mat1[0])):
        ar1 = []
        for j in range(len(mat1[1])):
            try:
                ar1.append([mat1[0][i], mat1[1][j], mat2[i][j]])
            except IndexError:
                ar1.append([mat1[0][i], mat1[1][j], None])
        ar.append(ar1)
    return ar

3维矩阵的另一个示例:

def my_matrix_3d(mat1, mat2):
    ar = []
    for i in range(len(mat1[0])):
        ar1 = []
        for j in range(len(mat1[1])):
            ar2 = []
            for k in range(len(mat1[2])):
                try:
                    ar2.append([mat1[0][i], mat1[1][j], mat1[2][k], mat2[i][j][k]])
                except IndexError:
                    ar2.append([mat1[0][i], mat1[1][j], mat1[2][k], None])
            ar1.append(ar2)
        ar.append(ar1)
    return ar

我需要对n维矩阵做一个通用函数。我知道递归一定要解决,但是我只是想不通。 我将不胜感激。

编辑:

实际上我没有很好地描述我的问题,因此我将尝试给出详细的描述。首先,我将描述2D问题。我有两个矩阵,mat1和mat2。 len(mat1)= 2,假设len(mat1 [0])= m和len(mat1 [1])= n。然后,mat2是m x n矩阵,因此对于i = 0,1,...,m-1,len(mat2)= m和len(mat2 [i])= n。 my_matrix_2d是函数,以某种方式组合了这两个矩阵。让我们看一个具体的例子:我们可以看到mat1是2D直方图的bin边缘,mat2是bin的高度,因此mat1 [0]代表x坐标的bin边缘,mat1 [1]代表y坐标的bin边缘。 mat2表示在x坐标和y坐标边缘相交处的仓高。因此,my_matrix_2d在二维直方图中存储有关箱的边缘和箱的高度的信息。它的元素是向量(mat1 [0] [i],mat1 [1] [j],mat2 [i] [j]),由于我们还需要一个边来确定垃圾箱宽度,因此最后一个向量是(mat1 [ 0] [i],mat1 [1] [j],无)。

与3D示例类似。 mat1 [0](len = m)是x坐标的bin边缘,mat1 [1](len = n)是y坐标的bin边缘,mat1 [2](len = o)是z坐标的bin边缘,而mat2是mxnxo矩阵,其中包含箱高度。 my_matrix_3d输出应为具有元素(矩阵1 [0] [i],矩阵1 [1] [j],矩阵1 [2] [k],矩阵2 [i] [j] [k])的矩阵/数组,其中i = 0,...,m-2,j = 0,...,n-2,k = 0,...,o-2,如果i = m-1或j = n-1或k = o-1,矩阵中的元素应为(mat1 [0] [i],mat1 [1] [j],mat1 [2] [k],无)。

P.S。 my_matrix_nd应该是n维矩阵,其形状与mat2相同。但是mat2中的元素是浮点数,而my_matrix_nd中的元素是len(mat1)+1个向量。

EDIT2:

我举了具体的例子(2D):

mat1 = [
array([-82.8894492, -56.07043142, -29.25141365, -2.43239587, 24.3866219,  51.20563967,  78.02465745]),
array([-191.15188724, -103.20853717, -15.2651871, 72.67816297, 160.62151304, 248.56486311])
]

mat2 = array([[4.23988548e-08, 3.39190838e-07, 1.27196564e-07, 1.27196564e-07, 0.00000000e+00],
[1.22956679e-06, 8.35257440e-06, 1.53483854e-05, 4.45187976e-06,
1.69595419e-07],
[5.00306487e-06, 4.77835094e-05, 8.47553108e-05, 2.31073759e-05,
1.05997137e-06],
[5.13026143e-06, 5.80016334e-05, 9.37862668e-05, 2.62872900e-05,
1.01757252e-06],
[1.14476908e-06, 1.44156106e-05, 2.33617690e-05, 6.52942364e-06,
2.54393129e-07],
[0.00000000e+00, 5.93583967e-07, 1.10237023e-06, 4.23988548e-07,
4.23988548e-08]])

output:
[

[[-82.88944919513716, -191.1518872423128, 4.2398854812237576e-08], 
[-82.88944919513716, -103.20853717269506, 3.391908384979006e-07], 
[-82.88944919513716, -15.265187103077324, 1.2719656443671272e-07], 
[-82.88944919513716, 72.67816296654041, 1.2719656443671272e-07], 
[-82.88944919513716, 160.62151303615815, 0.0],
[-82.88944919513716, 248.56486310577586, None]],

[[-56.07043142169904, -191.1518872423128, 1.2295667895548898e-06], 
[-56.07043142169904, -103.20853717269506, 8.352574398010803e-06], 
[-56.07043142169904, -15.265187103077324, 1.534838544203e-05], 
[-56.07043142169904, 72.67816296654041, 4.451879755284945e-06], 
[-56.07043142169904, 160.62151303615815, 1.6959541924895036e-07], 
[-56.07043142169904, 248.56486310577586, None]],

[[-29.25141364826092, -191.1518872423128, 5.003064867844034e-06], 
[-29.25141364826092, -103.20853717269506, 4.7783509373391746e-05], 
[-29.25141364826092, -15.265187103077324, 8.475531076966292e-05], 
[-29.25141364826092, 72.67816296654041, 2.3107375872669477e-05], 
[-29.25141364826092, 160.62151303615815, 1.0599713703059398e-06], 
[-29.25141364826092, 248.56486310577586, None]],

[[-2.4323958748228023, -191.1518872423128, 5.130261432280746e-06], 
[-2.4323958748228023, -103.20853717269506, 5.8001633383141e-05], 
[-2.4323958748228023, -15.265187103077324, 9.378626684466952e-05], 
[-2.4323958748228023, 72.67816296654041, 2.628728998358729e-05], 
[-2.4323958748228023, 160.62151303615815, 1.0175725154937022e-06], 
[-2.4323958748228023, 248.56486310577586,None]],

[[24.386621898615317, -191.1518872423128, 1.144769079930414e-06], 
[24.386621898615317, -103.20853717269506, 1.4415610636160767e-05], 
[24.386621898615317, -15.265187103077324, 2.336176900154289e-05], 
[24.386621898615317, 72.67816296654041, 6.529423641084582e-06], 
[24.386621898615317, 160.62151303615815, 2.543931288734254e-07], 
[24.386621898615317, 248.56486310577586, None]],

[[51.20563967205345, -191.1518872423128, 0.0],
[51.20563967205345, -103.20853717269506, 5.935839673713263e-07], 
[51.20563967205345, -15.265187103077324, 1.1023702251181777e-06],
[51.20563967205345, 72.67816296654041, 4.2398854812237604e-07], 
[51.20563967205345, 160.62151303615815, 4.2398854812237616e-08], 
[51.20563967205345, 248.56486310577586, None]],

[[78.02465744549156, -191.1518872423128, None],
[78.02465744549156, -103.20853717269506, None],
[78.02465744549156, -15.265187103077324, None],
[78.02465744549156, 72.67816296654041, None],
[78.02465744549156, 160.62151303615815, None],
[78.02465744549156, 248.56486310577586, None]]

]

1 个答案:

答案 0 :(得分:0)

递归不太复杂:

def lookup(m,ii):
  try:
    for i in ii: m=m[i]
  except IndexError: m=None
  return m
def edge_product(mat1,mat2,path=[]):
  return [edge_product(mat1,mat2,path+[i])\
          for i in range(len(mat1[len(path)]))]\
    if len(path)<len(mat1) else\
    [mat1[i] for i in path]+[lookup(mat2,path)]

请注意,可变的默认参数在这里并不是一个错误:它永远不会突变。