【发布时间】:2020-05-15 03:36:18
【问题描述】:
我有一个问题,我已经跑了很多圈,但无法找到最佳解决方案,情况是:
1.我已经创建了一个效果很好的循环,那么我必须退出循环,因为我已经达到了我需要的结果。
2.我必须再次调用循环才能对我的第一个循环的结果进行其他操作,但我需要在前一个循环中创建的数组“index_val”,问题是为了让第二部分正常工作,我必须重复代码,(与我现在评论过的相同)。
import numpy as np
data = [[ 7, 8, 9, 1, 2, 3],
[ 10, 11, 12, 4, 5, 6],
[ 1, 2, 3, 4, 5, 6]]
index = []
Vector_master = np.zeros((12,1))
Matrix_master = np.zeros((12,12))
for i in range(len(data)):
Vector_n = np.zeros((12,1))
Matrix_n = np.zeros((12,12))
index.append([data[i][0], data[i][1], data[i][2],
data[i][3], data[i][4], data[i][5]])
index_val = np.array([index[i]])
index_val.shape = (index_val.size,)
index_val -= 1
print(index_val)
w = data[i][0]*data[i][1]
L = data[i][3]
s_elemental = np.array([0, w/2, w**2, 0, w/2, -w**2])
Vector_n[index_val, 0] = s_elemental
for row in range(12):
Vector_master[row]+=Vector_n[row]
matrix = np.array([[ L, 0, 0, L, 0, 0],
[ 0, L**3, L**2, 0, L**3, L**2],
[ 0, L**2, L, 0, L**2, L],
[ -L, 0, 0, L, 0, 0],
[ 0, L**3, L**2, 0, L**3, L**2],
[ 0, L**2, L, 0, L**2, L]])
Matrix_n[np.ix_(index_val, index_val)] = matrix
for row in range(12):
for col in range(12):
Matrix_master[row][col] += Matrix_n[row][col]
k11 = Matrix_master[ :6 , :6]
Vector_cut = Vector_master[ :6]
vector_a = np.dot(k11, Vector_cut)
Vector_b = np.zeros((6,1))
vector_c = np.append(vector_a, Vector_b)
print('\n')
for i in range(len(data)):
# index.append([data[i][0], data[i][1], data[i][2],
# data[i][3], data[i][4], data[i][5]])
# index_val = np.array([index[i]])
# index_val.shape = (index_val.size,)
# index_val -= 1
print('\n',index_val)
vector_result = vector_c[index_val]
print(vector_result)
目前,我在屏幕上打印的内容如下:
[0 1 2 3 4 5]
[ 0. -241439. -62687. 0. -428416. -107104.]
[0 1 2 3 4 5]
[ 0. -241439. -62687. 0. -428416. -107104.]
[0 1 2 3 4 5]
[ 0. -241439. -62687. 0. -428416. -107104.]
你应该打印这个:
[6 7 8 0 1 2]
[0. 0. 0. 0. -241439. -62687.]
[9 10 11 3 4 5]
[0. 0. 0. 0. -428416. -107104.]
[0 1 2 3 4 5]
[0. -241439. -62687. 0. -428416. -107104.]
非常感谢您的帮助,请原谅我的英语不是我的母语,问候。
【问题讨论】:
标签: python arrays python-3.x loops