0

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

On a last post Stack', someone helps me to do it :

e = Elements[:,1:].ravel().astype(int)
n=nodes[:,0].astype(int)
I, J = np.where(e==n[:,None])
results = np.zeros((e.shape[0],2),nodes.dtype)
results[J] = nodes[I,1:]
np.concatenate((Elements[:,[0]],results.reshape(2,4)),axis=1)

It works here, but when i do on a great number of values, i got an error : "need more than 1 value to unpack" but i dont understand what is the origin of the problem...

--- EDIT LATER -----

I think it could be due to the importance of my arrays. Perhaps is there an other way to deal with this problem? (21536, 4) and Nodes_coord.shape : (10926, 3)

6
  • On which line of your code do you get the error? Commented Jan 4, 2016 at 10:19
  • On the " I, J = np.where(e==n[:,None])" I dont know if my values present something wrong... Commented Jan 4, 2016 at 10:20
  • Shapes of Elements and nodes for your actual case? Commented Jan 4, 2016 at 10:23
  • 2
    In the occasion that you get an error, I'm guessing that e==n[:,None] is never true, in which case np.where returns a tuple with only a single value, which you're trying to unpack into two values (I and J). Commented Jan 4, 2016 at 10:24
  • Table_connect.shape : (21536, 4) and Nodes_coord.shape : (10926, 3) But it is impossible to be never True :/ A strange value could be present in my data which provoks it? Commented Jan 4, 2016 at 10:24

1 Answer 1

1

It looks like you're using at least one element which does not have a corresponding node. Try this code to check if this is indeed the case:

element_ids = Elements[:, 1]
node_ids = nodes[:, 0]
valid = np.all([elem_id in node_ids for elem_id in element_ids ])

I'm guessing valid will be False.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for help and I get "True" :/
In that case, what does np.where(e==n[:,None]) evaluate to?
It returns (array([], dtype=int32),) very strange :/
This implies that elements in e are never equal to elements in n[:, None], which implies that your elements do not have matching nodes.
Yep but it seems to be impossible :/ thanks for your help ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.