用第二个数组的2个值替换一个数组的数据 [英] Replace data of an array by 2 values of a second array

查看:121
本文介绍了用第二个数组的2个值替换一个数组的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个numpy数组元素"和节点".我的目的是聚集这些数组的一些数据.我需要通过节点"数组中包含的两个坐标来替换最后两个列的元素"数据.这两个数组非常大,我必须使其自动化.

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.

一个例子:

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.]])

我认为使用"if"和"for循环"可以做到这一点,但我不知道如何附加结果...

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:

输入:

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.]])

Elements 中的ID作为矢量;将其设置为 int 以便于比较:

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])

来自 nodes 的相似ID:

In [119]: n=nodes[:,0].astype(int)
In [120]: n
Out[120]: array([11, 12, 13, 14])

使用广播将 e n 进行比较-制作4x4的True/False数组.使用 where 查找其坐标:

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])

神奇的是,我们现在可以将节点ID与元素ID进行匹配.如果此操作不清楚,请打印一些中间数组.

And magically we can now match up node ids with elements ids. Print some intermediate arrays if this action is unclear.

制作一个 results 数组,每个 e 元素一行,然后复制相应的 nodes 值.

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.]])

其中进行基本匹配.剩下的大部分只是改型和类型转换,以处理以下事实:我们需要填充的插槽"是3列Elements数组中的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.

出于好奇,我想出了如何使用Elements id而不用担心的事情:

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.]])

这篇关于用第二个数组的2个值替换一个数组的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆