how to improve this cycle (extracting data from structured array)?

A

Alexzive

Hello guys,

I am just wondering if there is a quick way to improve this algorithm
[N is a structured array which hold info about the nodes n of a finite
element mesh, and n is about 300.000). I need to extract info from N
and put it in to a 3*n matrix NN which I reshape then with numpy. I
think to "append" is quite unefficient:

***********************************************************
N = odb.rootAssembly.instances['PART-1-1'].nodes


NN=[]

B=[0,0,0]
#auxsiliar vector
for i in range(len(N)):

B[0] = N.label

B[1] = N.coordinates[0]

B[2] = N.coordinates[1]

NN = append(NN,B)


NN=NN.reshape(-1,3)

****************************************************
Many Thanks in advance!
Alex
 
T

Tim Chase

Alexzive said:
I am just wondering if there is a quick way to improve this algorithm
[N is a structured array which hold info about the nodes n of a finite
element mesh, and n is about 300.000). I need to extract info from N
and put it in to a 3*n matrix NN which I reshape then with numpy. I
think to "append" is quite unefficient:

***********************************************************
N = odb.rootAssembly.instances['PART-1-1'].nodes

NN=[]

B=[0,0,0]
#auxsiliar vector
for i in range(len(N)):
B[0] = N.label
B[1] = N.coordinates[0]
B[2] = N.coordinates[1]
NN = append(NN,B)


Usually this would be written with a list-comprehension,
something like

nn = [(x.label, x.coordinates[0], x.coordinates[1])
for x in N]

or if you really need lists-of-lists instead of lists-of-tuples:

nn = [[x.label, x.coordinates[0], x.coordinates[1]]
for x in N]

-tkc
 
A

Alexzive

Alexzive said:
I am just wondering if there is a quick way to improve this algorithm
[N is a structured array which hold info about the nodes n of a finite
element mesh, and n is about 300.000). I need to extract info from N
and put it in to a 3*n matrix NN which I reshape then with numpy. I
think to "append" is quite unefficient:
***********************************************************
N = odb.rootAssembly.instances['PART-1-1'].nodes

B=[0,0,0]
 #auxsiliar vector
for i in range(len(N)):
    B[0] = N.label
    B[1] = N.coordinates[0]
    B[2] = N.coordinates[1]
    NN = append(NN,B)


Usually this would be written with a list-comprehension,
something like

   nn = [(x.label, x.coordinates[0], x.coordinates[1])
         for x in N]

or if you really need lists-of-lists instead of lists-of-tuples:

   nn = [[x.label, x.coordinates[0], x.coordinates[1]]
         for x in N]

-tkc


yeah, much better thanks!
Alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top