在Python中添加多个数组 [英] Add multiple arrays in Python
问题描述
我到目前为止创建的内容
我创建了一个零为18x18的正方形矩阵,称为"master_matrix".
I have created an 18x18 square matrix of zeros called ‘master_matrix’.
我创建了一个名为 ingreso_datos 的数组,其列0 [col 0]表示数据标签.
I have created an array called ingreso_datos, whose column 0 [col 0] indicates the data label.
我创建了一个for循环,其中:
I have created a for loop where:
对于每个数据标签,我都会有一个 little_matrix ,其值将在其相应的行和列中分配给 master_matrix .由于这是在for循环内发生的,因此在此示例中,我得到6个 master_matrix (在我的变量列表中,仅读取了最后一个,即第六个 master_matrix ).
For each data label I will have a little_matrix whose values will be assigned to master_matrix in their corresponding rows and columns. As this happens inside a for loop, for this example I get 6 master_matrix (in my variable list only the last one is read, that is, the sixth master_matrix).
我需要做什么
我现在正在寻找的是添加来自for循环的6个 master_matrix .
What I'm looking for now is to add the 6 master_matrix that come from the for loop.
我已经尝试过一个接一个地做,但是问题是数据可能会更改,我认为效率不高,非常感谢您的帮助.
I have tried to do it one by one but the problem is that the data could change and I think it would not be efficient, I would greatly appreciate your help, regards.
import numpy as np
我创建了一个18x18的零平方矩阵,称为"master_matrix"
I have created an 18x18 square matrix of zeros called ‘master_matrix’
master_matrix = np.zeros((18, 18))
我创建了一个名为'ingreso_datos'的数组,其列0 [列0]表示数据标签.
I have created an array called 'ingreso_datos', whose column 0 [col 0] indicates the data label.
# Data label |-------1(i) 2(i) 3(i) 1(j) 2(j) 3(j) --|
# [Col0] |------[Col1] [Col2] [Col3] [Col4] [Col5] [Col6] --|
ingreso_datos = [[ 1, 13, 14, 15, 7, 8, 9],
[ 2, 16, 17, 18, 10, 11, 12],
[ 3, 7, 8, 9, 1, 2, 3],
[ 4, 10, 11, 12, 4, 5, 6],
[ 5, 7, 8, 9, 10, 11, 12],
[ 6, 1, 2, 3, 4, 5, 6]]
对于每个数据标签,我都会有一个"little_matrix",其值将分配给"master_matrix"在其相应的行和列中.因为这发生在for循环中,所以在本示例中,我得到6"master_matrix"(在我的变量列表中,仅读取最后一个,即第六个"master_matrix").
For each data label I will have a 'little_matrix' whose values will be assigned to 'master_matrix' in their corresponding rows and columns. As this happens inside a for loop, for this example I get 6 'master_matrix' (in my variable list only the last one is read, that is, the sixth 'master_matrix').
indices = [] # moved outside of the loop
for i in range(len(ingreso_datos)):
indices.append([ingreso_datos[i][0], ingreso_datos[i][1], ingreso_datos[i][2], ingreso_datos[i][3],
ingreso_datos[i][4], ingreso_datos[i][5], ingreso_datos[i][6]])
for row in indices:
indices = np.array(row[1:])
indices -= 1
d = 5
s = 0.2
e = 0.05
y = 5000000
little_matrix = np.array([[ s*y/d, 0, 0, -s*y/d, 0, 0],
[ 0, y*e/d**3, y*e/d**2, 0, -y*e/d**3, y*e/d**2],
[ 0, y*e/d**2, y*e/d, 0, -y*e/d**2, y*e/d],
[-s*y/d, 0, 0, s*y/d, 0, 0],
[ 0, y*e/d**3, -y*e/d**2, 0, y*e/d**3, -y*e/d**2],
[ 0, y*e/d**2, y*e/d, 0, -y*e/d**2, y*e/d]])
master_matrix[np.ix_(indices, indices)] = little_matrix
我现在正在寻找的是添加来自for循环的6个master_matrix.
What I'm looking for now is to add the 6 master_matrix that come from the for loop.
master_matrix.sum()
推荐答案
In [68]: ingreso_datos = np.array([[ 1, 13, 14, 15, 7,
...: 8, 9],
...: [ 2, 16, 17, 18, 10, 11,
...: 12],
...: [ 3, 7, 8, 9, 1, 2,
...: 3],
...: [ 4, 10, 11, 12, 4, 5,
...: 6],
...: [ 5, 7, 8, 9, 10, 11,
...: 12],
...: [ 6, 1, 2, 3, 4, 5,
...: 6]])
In [69]: ingreso_datos.shape
Out[69]: (6, 7)
创建 indices
并没有做任何有意义的事情:
That indices
creation doesn't do anything significant:
In [70]: indices = [] # moved outside of the loop
...: for i in range(len(ingreso_datos)):
...: indices.append([ingreso_datos[i][0], ingreso_datos[i][1], ingreso_datos[i][2], ing
...: reso_datos[i][3],
...: ingreso_datos[i][4], ingreso_datos[i][5], ingreso_datos[i][6]])
...:
...:
In [71]: np.array(indices).shape
Out[71]: (6, 7)
In [72]: np.allclose(ingreso_datos, np.array(indices))
Out[72]: True
little_matrix
不需要在循环中,因为它不会改变:
little_matrix
doesn't need to be in the loop, since it doesn't change:
In [74]: little_matrix
Out[74]:
array([[ 200000., 0., 0., -200000., 0., 0.],
[ 0., 2000., 10000., 0., -2000., 10000.],
[ 0., 10000., 50000., 0., -10000., 50000.],
[-200000., 0., 0., 200000., 0., 0.],
[ 0., 2000., -10000., 0., 2000., -10000.],
[ 0., 10000., 50000., 0., -10000., 50000.]])
关于您在 row
上的迭代:
In [75]: np.array(indices[0][1:])-1
Out[75]: array([12, 13, 14, 6, 7, 8])
In [77]: ingreso_datos[0,1:]-1
Out[77]: array([12, 13, 14, 6, 7, 8])
因此您对 master_matrix
的迭代分配变为:
So your iterative assignment to master_matrix
becomes:
In [78]: master_matrix=np.zeros((18,18))
In [80]: for row in ingreso_datos:
...: indices = row[1:]-1
...: master_matrix[np.ix_(indices,indices)]=little_matrix
...:
In [81]: master_matrix
Out[81]:
array([[ 200000., 0., 0., -200000., 0., 0.,
-200000., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0.],
[ 0., 2000., 10000., 0., -2000., 10000.,
0., 2000., -10000., 0., 0., 0.,
0., 0., 0., 0., 0., 0.],
...
我怀疑我们也可以消除该迭代,但这将需要更多工作.
I suspect we can eliminate that iteration as well, but that'll take some more work.
查看整个索引:
In [88]: x = ingreso_datos[:,1:]-1
In [89]: x
Out[89]:
array([[12, 13, 14, 6, 7, 8],
[15, 16, 17, 9, 10, 11],
[ 6, 7, 8, 0, 1, 2],
[ 9, 10, 11, 3, 4, 5],
[ 6, 7, 8, 9, 10, 11],
[ 0, 1, 2, 3, 4, 5]])
我看到一些重叠.例如,最后一行覆盖了第2行写入的一些值:
I see some overlap. For example the last row over writes some of the values written by row 2:
In [90]: master_matrix[:6,:6]
Out[90]:
array([[ 200000., 0., 0., -200000., 0., 0.],
[ 0., 2000., 10000., 0., -2000., 10000.],
[ 0., 10000., 50000., 0., -10000., 50000.],
[-200000., 0., 0., 200000., 0., 0.],
[ 0., 2000., -10000., 0., 2000., -10000.],
[ 0., 10000., 50000., 0., -10000., 50000.]])
In [91]: master_matrix[np.ix_(x[2],x[2])]
Out[91]:
array([[ 200000., 0., 0., -200000., 0., 0.],
[ 0., 2000., 10000., 0., -2000., 10000.],
[ 0., 10000., 50000., 0., -10000., 50000.],
[-200000., 0., 0., 200000., 0., 0.],
[ 0., 2000., -10000., 0., 2000., 10000.],
[ 0., 10000., 50000., 0., 10000., 50000.]])
(请注意,右下角的符号已更改).
(notice the sign change in the bottom right corner).
考虑到这一点,不可能一次调用来复制迭代分配.
Given that over lap it's impossible to replicate the iterative assignment with one call.
这篇关于在Python中添加多个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!