0

I have two for loops that generate an array with the shape of (1, 3).

I want to vertically stack the result of each iteration in an array. More clearly, the final output should be a (3, 3) array where the first row is the out of the first iteration and so on so forth. My code looks like below :

for i in range (2):


    for j in range (i, 2):


            yparam = The_path / ("{}_Y{}{}.csv" .format('Filter',i+1,j+1) )


            with open( yparam ,'r', newline='') as Y_parameter_Data:
                    data =  numpy.loadtxt(Y_parameter_Data, skiprows=1, delimiter=',')

                    ypar = []

                    for k in range(0, len(3)):
                            a = numpy.reshape(data[k,1::2], (1,1))
                            b = numpy.reshape(data[k,2::2], (1,1))
                            ypar.append(a+j*b)

                    c = ypar

                    fdata= numpy.asarray(c) 
                    fdata= numpy.reshape(fdata, (1,-1))


                    fdata= fdata

The problem here is that how can I save the fdata for each iteration. Then I can use numpy.vstack to stack the results. Thanks for any help.

Hey There
  • 275
  • 3
  • 14

1 Answers1

1

Try defining a list consisting of lists.

for i in range (2):
    InnerList=[]
    for j in range (i, 2):
            yparam = The_path / ("{}_Y{}{}.csv" .format('Filter',i+1,j+1) )
            with open( yparam ,'r', newline='') as Y_parameter_Data:
                data =  numpy.loadtxt(Y_parameter_Data, skiprows=1, delimiter=',')
                ypar = []
                for k in range(0, len(3)):
                        a = numpy.reshape(data[k,1::2], (1,1))
                        b = numpy.reshape(data[k,2::2], (1,1))
                        ypar.append(a+j*b)
                c = ypar

                fdata= numpy.asarray(c) 
                fdata= numpy.reshape(fdata, (1,-1))


                fdata= fdata
                InnerList.append(fdata)
    OuterList.append(InnerList)
Keyinator
  • 111
  • 2
  • 10