构造限制矩阵使得奇数行为零
Construct restriction matrix such that the odd rows are zero
我有两个数组:A
的形状为 (m,n)
,B
的形状为 (2*m,n)
。此外,B
的偶数行是A
的行。让a = numpy.reshape(A,(A.size,1))
和b = numpy.reshape(B,(B.size,1))
。我的目标是构造一个对角矩阵 R
使得 R*b = a
。数组如下所示:
[[13865.995 14020.785 14020.788 ]
[ 6885.634 6784.8813 6837.428 ]
[14510.425 14712.554 14657.217 ]
[ 7688.0923 7817.8457 7792.413 ]
[10473.903 10417.55 10469.508 ]
[10485.661 10348.152 10632.414 ]]
[[14662.705 14869.951 15166.294 ]
[13865.995 14020.785 14020.788 ]
[ 9780.559 10038.395 10202.31 ]
[ 6885.634 6784.8813 6837.428 ]
[ 7167.9575 7357.9062 7287.3003]
[14510.425 14712.554 14657.217 ]
[12825.017 12680.751 12823.563 ]
[ 7688.0923 7817.8457 7792.413 ]
[ 6861.9443 6826.6245 6758.8965]
[10473.903 10417.55 10469.508 ]
[ 8498.976 8637.245 8718.052 ]
[10485.661 10348.152 10632.414 ]]
我确定我可以在正确的索引处构造一个数组 x
,其值为 0
和 1
,然后设置 R = np.diag(x)
,但我不确定我怎样才能使这项工作,如果这是最好的解决方案。感谢任何帮助
这需要一些创造性的数组索引,但希望这就是您所追求的。注意:在这种情况下,R 实际上不是对角矩阵。
import numpy
A = numpy.array([[13865.995, 14020.785, 14020.788 ],
[ 6885.634, 6784.8813, 6837.428 ],
[14510.425, 14712.554, 14657.217 ],
[ 7688.0923, 7817.8457, 7792.413 ],
[10473.903, 10417.55, 10469.508 ],
[10485.661, 10348.152, 10632.414 ]])
B = numpy.array([[14662.705, 14869.951, 15166.294 ],
[13865.995, 14020.785, 14020.788 ],
[ 9780.559, 10038.395, 10202.31 ],
[ 6885.634, 6784.8813, 6837.428 ],
[ 7167.9575, 7357.9062, 7287.3003],
[14510.425, 14712.554, 14657.217 ],
[12825.017, 12680.751, 12823.563 ],
[ 7688.0923, 7817.8457, 7792.413 ],
[ 6861.9443, 6826.6245, 6758.8965],
[10473.903, 10417.55, 10469.508 ],
[ 8498.976, 8637.245, 8718.052 ],
[10485.661, 10348.152, 10632.414 ]])
a = numpy.reshape(A,(A.size,1))
b = numpy.reshape(B,(B.size,1))
R = numpy.zeros((A.size,B.size))
R[[a for a in range(A.size)],[a for a in range(B.size) if (a // 3) % 2]] = 1
print( numpy.all(numpy.dot(R, b) == a) )
# True
我有两个数组:A
的形状为 (m,n)
,B
的形状为 (2*m,n)
。此外,B
的偶数行是A
的行。让a = numpy.reshape(A,(A.size,1))
和b = numpy.reshape(B,(B.size,1))
。我的目标是构造一个对角矩阵 R
使得 R*b = a
。数组如下所示:
[[13865.995 14020.785 14020.788 ]
[ 6885.634 6784.8813 6837.428 ]
[14510.425 14712.554 14657.217 ]
[ 7688.0923 7817.8457 7792.413 ]
[10473.903 10417.55 10469.508 ]
[10485.661 10348.152 10632.414 ]]
[[14662.705 14869.951 15166.294 ]
[13865.995 14020.785 14020.788 ]
[ 9780.559 10038.395 10202.31 ]
[ 6885.634 6784.8813 6837.428 ]
[ 7167.9575 7357.9062 7287.3003]
[14510.425 14712.554 14657.217 ]
[12825.017 12680.751 12823.563 ]
[ 7688.0923 7817.8457 7792.413 ]
[ 6861.9443 6826.6245 6758.8965]
[10473.903 10417.55 10469.508 ]
[ 8498.976 8637.245 8718.052 ]
[10485.661 10348.152 10632.414 ]]
我确定我可以在正确的索引处构造一个数组 x
,其值为 0
和 1
,然后设置 R = np.diag(x)
,但我不确定我怎样才能使这项工作,如果这是最好的解决方案。感谢任何帮助
这需要一些创造性的数组索引,但希望这就是您所追求的。注意:在这种情况下,R 实际上不是对角矩阵。
import numpy
A = numpy.array([[13865.995, 14020.785, 14020.788 ],
[ 6885.634, 6784.8813, 6837.428 ],
[14510.425, 14712.554, 14657.217 ],
[ 7688.0923, 7817.8457, 7792.413 ],
[10473.903, 10417.55, 10469.508 ],
[10485.661, 10348.152, 10632.414 ]])
B = numpy.array([[14662.705, 14869.951, 15166.294 ],
[13865.995, 14020.785, 14020.788 ],
[ 9780.559, 10038.395, 10202.31 ],
[ 6885.634, 6784.8813, 6837.428 ],
[ 7167.9575, 7357.9062, 7287.3003],
[14510.425, 14712.554, 14657.217 ],
[12825.017, 12680.751, 12823.563 ],
[ 7688.0923, 7817.8457, 7792.413 ],
[ 6861.9443, 6826.6245, 6758.8965],
[10473.903, 10417.55, 10469.508 ],
[ 8498.976, 8637.245, 8718.052 ],
[10485.661, 10348.152, 10632.414 ]])
a = numpy.reshape(A,(A.size,1))
b = numpy.reshape(B,(B.size,1))
R = numpy.zeros((A.size,B.size))
R[[a for a in range(A.size)],[a for a in range(B.size) if (a // 3) % 2]] = 1
print( numpy.all(numpy.dot(R, b) == a) )
# True