Python的 - 由第二阵列的2值替换的数组数据 [英] Python - Replace data of an array by 2 values of a second array
问题描述
我有两个numpy的阵列元素和节点。我的目的是收集
这些阵列的一些数据。
我需要用两个坐标来remplace最后两栏的元素的数据包含在节点阵列。
这两个数组是非常巨大的,我要自动执行它。
一个例子:
导入numpy的是NP元素= np.array([[1,11,14],[2,12,13。]])节点= np.array([11,0,0],[12,1,1],[13,2,2],[14,3,3。] ])结果= np.array([[1,0,0,3,3]
[2. 1.,1.,2,2]])
我想用如果和循环就可以做到这一点,但我不知道如何追加结果...
检测= []
因为我在范围内(Elements.shape [0]):
如果(元素[:,:1] ==节点[I,0]):
下面是不使用循环的一个版本:
的输入:
在[115]:元素= np.array([[1,11,14],[2,12,13])
在[116]:节点= np.array([11,0,0],[12,1,1],[13,2,2],[14,3。 ,3。]])
从元素的ID
作为载体;让 INT
为便于比较:
在[117]:E =元素[:,1:]。拉威尔()astype(INT)
在[118]:电子
出[118]:阵列([11,14,12,13])
从节点
类似的标识:
在[119]:N =节点[:0] .astype(INT)
在[120]:N
出[120]:阵列([11,12,13,14])
比较电子
与 N
使用广播 - 这使得真/假一个4x4的阵列。使用其中,
来找到自己的坐标:
在[121]:I,J = np.where(E == N [:,无])
在[122]:我
出[122]:阵列([0,1,2,3],DTYPE = INT32)
在[123]:J-
出[123]:阵列([0,2,3,1],DTYPE = INT32)
在[124]:E [J]。
出[124]:阵列([11,12,13,14])
在[125]:N [I]
出[125]:阵列([11,12,13,14])
和神奇,我们现在可以匹配的元素IDS节点ID。打印一些中间数组,如果这个动作目前尚不清楚。
请在结果
数组,每个电子
元素一行,并复制相应的节点
值了。
在[131]:结果= np.zeros((e.shape [0],2),nodes.dtype)
在[132]:结果[J] =节点[I,1:]
在[133]:结果
出[133]:
阵列([0,0],
[3,3]
[1,1]
[2. 2.]])
加入结果
的元素的初始列:
在[134]:np.concatenate((元素[:,[0],results.reshape(2,4)),轴= 1)
出[134]:
阵列([1,0,0,3,3]
[2. 1.,1.,2,2]])
,其中
做的基本匹配。其余大部分是刚刚改造和类型转换来处理的事实,插槽我们需要填写是3列元素数组的2列。
只是出于好奇,我想通了如何使用IDS的元素不脱散:
在[149]:E2 =元素[:,1:] astype(INT)
在[150]:I,J,K = np.where(E2 == N [:,无,无])
在[151]:结果2 = np.zeros((e2.shape [0],e2.shape [1],2),nodes.dtype)
在[152]:结果2 [J,K] =节点[I,1:]
在[153]:results2.reshape(2,4)#还是需要一个重塑
出[153]:
阵列([0,0,3,3]
[1.,1.,2,2]])
I have two numpy arrays "Elements" and "nodes". My aim is to gather some data of these arrays. I need to remplace "Elements" data of the two last columns by the two coordinates contains in "nodes" array. The two arrays are very huge, i have to automate it.
An example :
import numpy as np
Elements = np.array([[1.,11.,14.],[2.,12.,13.]])
nodes = np.array([[11.,0.,0.],[12.,1.,1.],[13.,2.,2.],[14.,3.,3.]])
results = np.array([[1., 0., 0., 3., 3.],
[2., 1., 1., 2., 2.]])
I think with "if" and a "for loop" it is possible to do that but i dont know how to append the results...
test=[]
for i in range(Elements.shape[0]):
if (Elements[:,:1] == nodes[i,0]):
Here's a version that does not use a loop:
The inputs:
In [115]: Elements = np.array([[1.,11.,14.],[2.,12.,13.]])
In [116]: nodes = np.array([[11.,0.,0.],[12.,1.,1.],[13.,2.,2.],[14.,3.,3.]])
The ids from Elements
as a vector; make it int
for easy comparison:
In [117]: e = Elements[:,1:].ravel().astype(int)
In [118]: e
Out[118]: array([11, 14, 12, 13])
Similar ids from nodes
:
In [119]: n=nodes[:,0].astype(int)
In [120]: n
Out[120]: array([11, 12, 13, 14])
Compare e
with n
using broadcasting - that makes a 4x4 array of True/False. Use where
to find their coordinates:
In [121]: I, J = np.where(e==n[:,None])
In [122]: I
Out[122]: array([0, 1, 2, 3], dtype=int32)
In [123]: J
Out[123]: array([0, 2, 3, 1], dtype=int32)
In [124]: e[J]
Out[124]: array([11, 12, 13, 14])
In [125]: n[I]
Out[125]: array([11, 12, 13, 14])
And magically we can now match up node ids with elements ids. Print some intermediate arrays if this action is unclear.
Make a results
array, one row per element of e
, and copy the corresponding nodes
values over.
In [131]: results = np.zeros((e.shape[0],2),nodes.dtype)
In [132]: results[J] = nodes[I,1:]
In [133]: results
Out[133]:
array([[ 0., 0.],
[ 3., 3.],
[ 1., 1.],
[ 2., 2.]])
Join results
with the initial column of Elements:
In [134]: np.concatenate((Elements[:,[0]],results.reshape(2,4)),axis=1)
Out[134]:
array([[ 1., 0., 0., 3., 3.],
[ 2., 1., 1., 2., 2.]])
where
does the basic matching. Most of rest is just reshaping and type conversion to handle the fact that the 'slots' we need to fill are 2 columns of the 3 column Elements array.
Just out of curiousity, I figured how to use the Elements ids without raveling:
In [149]: e2 = Elements[:,1:].astype(int)
In [150]: I,J,K = np.where(e2==n[:,None,None])
In [151]: results2 = np.zeros((e2.shape[0],e2.shape[1],2),nodes.dtype)
In [152]: results2[J,K] = nodes[I,1:]
In [153]: results2.reshape(2,4) # still requires a reshape
Out[153]:
array([[ 0., 0., 3., 3.],
[ 1., 1., 2., 2.]])
这篇关于Python的 - 由第二阵列的2值替换的数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!