>我想计算满足条件的矩阵:result_x = 1 - initial_x
;result_y = initial_y
和results_z = initial_y
.
但是,我的代码只生成每个数组的最后一个。你能帮帮我吗?
import numpy as np
import math
def reverse_a_direction(matrix):
reverse_a = []
for (x, y, z) in matrix:
reverse_a = 1 - x, y, z
return reverse_a
a = np.array([[(0.1666666666666667, 0.8012144614989793, 0.7500000000000000),
(0.1666666666666667, 0.1987855385010207, 0.2500000000000000)],
[(0.6666666666666666, 0.3012144614989793, 0.7500000000000000),
(0.6666666666666666, 0.6987855385010207, 0.2500000000000000)]])
for i in range(0, len(a)):
print(reverse_a_direction(a[i]))
此代码的结果:
(0.8333333333333333, 0.1987855385010207, 0.25)
(0.3333333333333333, 0.6987855385010207, 0.25)
预期成果:
[(0.8333333333333333, 0.8012144614989793, 0.75), (0.8333333333333333, 0.1987855385010207, 0.25)],
[(0.3333333333333333, 0.3012144614989793, 0.75), (0.3333333333333333, 0.6987855385010207, 0.25)]
每次迭代都会覆盖reverse_a。正确的解决方案是:
def reverse_a_direction(matrix):
reverse_a = []
for (x, y, z) in matrix:
a = 1 - x, y, z
reverse_a.append(a)
return reverse_a
反向逻辑不会捕获结果,而是覆盖结果,因此您将最后一个结果保留在变量reverse_a[]
。
将作业从
reverse_a = 1 - x, y, z
自
reverse_a.append((1 - x, y, z))
给定您的原始数组:
X = np.array([
[
[0.1666666666666667, 0.8012144614989793, 0.7500000000000000],
[0.1666666666666667, 0.1987855385010207, 0.2500000000000000]
],
[
[0.6666666666666666, 0.3012144614989793, 0.7500000000000000],
[0.6666666666666666, 0.6987855385010207, 0.2500000000000000]
]
])
您可以像这样修改该轴:
Y = np.array(X)
Y[:,:,0] = 1 - Y[:,:,0]