我们可以制作md-matrix吗?

时间:2019-07-18 01:17:49

标签: python

  1. 使用python创建md矩阵。
  2. 重塑功能的原理是什么?

in和result应该类似于result

 in = (0, 1, 2, 3, 4, 5, 6, 7, 8)
tensor = (2, 3, 2)
result:
(((0, 1), (2, 3), (4, 5)), ((6, 7), (8, 0), (0, 0)))

1 个答案:

答案 0 :(得分:1)

尝试:

def multiply (arr):
    result = 1
    for x in arr:
        result *= x
    return result

def reshape(shape, arr):
    result = arr
    newSize = multiply(shape)
    if (len(arr) < newSize):
        while (len(arr) < newSize):
            result.append(0)
    else:
        result= result[:newSize]

    for s in shape[::-1]:
        result = [ result[i:i+s] for i in range(0,len(result),s)]
    return result

data = [0, 1, 2, 3, 4, 5, 6, 7, 8]
shape = [2, 3, 2]

print(reshape(shape, data))

data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] 
shape = [6, 2]

print(reshape(shape, data))