Question

I need to create a matrix in Python containing a list of unknown arrays r that have this form:

r_i=[r1,r2,r3,r4,th_12,th_13]

I am running for statements with a couple of if conditions that will give me in output a number of r_i arrays that I don't know from the beginning.

I am looking for a function like append that I normally use to create a vector with all the solutions that I generate, but this time each solution is not a single value but an array of 6 values and I am not able to generate what I want.

I need to create a matrix like this one, where every r_1 has the form of the code that I wrote above.

enter image description here

EDIT: I would like to generate a numpy array (R_tot should be a numpy array).

Was it helpful?

Solution

You can generate the array normally as I explained in my comment:

r_tot = []

for r_i in however_many_rs_there_are: # each r_i contains an array of 6 values
    r_tot.append(r_i)

You can then convert r_tot into a numpy array like so:

import numpy
np_array = numpy.array(r_tot)

Here's a very simple proof of concept:

>>> import random, numpy
>>> r_tot = []
>>> for i in range(0,random.randint(1,20)): # append an arbitrary number of arrays
        r_i = [1,2,3,4,5,6]                 # all of size six
        r_tot.append(r_i)                   # to r_tot

>>> np_array = numpy.array(r_tot)           # then convert to numpy array!
>>> np_array                                # did it work?
array([[1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],                  # yeaaaah
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6],
       [1, 2, 3, 4, 5, 6]])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top