python - 我们可以制作md-matrix吗?
问题描述
- 用python制作md矩阵。
- reshape函数的原理是什么?
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)))
解决方案
尝试:
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))
推荐阅读
- javascript - Realm 在其他模型中继承基础模型
- matlab - 在 MATLAB 中对包含 circshift 的多个嵌套循环进行向量化
- c# - 如何从 Exchange Server 获取没有默认邮箱(我没有默认邮箱)的用户的邮箱电子邮件详细信息?
- java - Java FTPS 禁用证书检查
- wordpress - 由于双击广告 dot.gif 导致的混合内容错误
- angular-reactive-forms - 当 formcontrol 位于 select 标签上时,selected 属性不起作用
- php - 在 PHP 文件中输入 /**/
- spring - 在 arangodb-spring-data 中启用 javax.validation.constraints
- c# - 创建多个用户时 UserManager 速度变慢,如何在我的操作中为每个用户创建再次处理和实例化?
- java - Btrace:按应用程序过滤具有相同名称的类