Home > Mobile >  Storing numpy.ndarrays from a loop
Storing numpy.ndarrays from a loop

Time:10-21

I am trying to store the numpy.ndarrays defined as x_c, y_c, and z_c for every iteration of the loop:

for z_value in np.arange(0, 5, 1):
    ms.set_current_mesh(0) 
    planeoffset : float = z_value 
    ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
    m = ms.current_mesh()
    matrix_name = m.vertex_matrix()
    x_c = matrix_name[:,0]
    y_c = matrix_name[:,1]
    z_c = matrix_name[:,2]

I would like to be able to recall the three arrays at any z_value, preferably with reference to the z_value i.e x_c @ z_value = 2 or similar.

Thanks for any help!

p.s very new to coding, so please go easy on me.

CodePudding user response:

You have to store each array in an external variable, for example a dictionary

x_c={}
y_c={}
z_c={}
for z_value in np.arange(0, 5, 1):
    ms.set_current_mesh(0)
    planeoffset = float(z_value)
    ms.compute_planar_section(planeaxis = 'Z Axis', planeoffset = planeoffset)
    m = ms.current_mesh()
    m.compact()
    print(m.vertex_number(), "vertices in Planar Section Z =", planeoffset)

    matrix_name = m.vertex_matrix()
    x_c[planeoffset] = matrix_name[:,0]
    y_c[planeoffset] = matrix_name[:,1]
    z_c[planeoffset] = matrix_name[:,2]

Please, ensure you call m.compact() before accessing the vertex_matrix or you will get a MissingCompactnessException error. Please, note that it is not the same to store anything in x_c[2] or in x_c[2.0], so choose if your index has to be integers o floats and keep the same type (in this example, they are floats).

Later, you can recall values like this:


print("X Values with z=2.0")
print(x_c[2.0])
  • Related