Appending a Matrix in Python within loop

Viewed 9500

I have a matrix y with size (3,3). Say it is a 3 by 3 matrix with all elements = 1.

I then have a loop to create multiple (3,3) matrices. So these are the outputs:

First loop I get this matrix:

 [[  88.    42.5    9. ]
 [ 121.5   76.    42.5]
 [ 167.   121.5   88. ]]

The second loop I get:

 [[  88.    42.5   13. ]
 [ 117.5   72.    42.5]
 [ 163.   117.5   88. ]]

So what I would like to achieve is essentially

 [[1, 1, 1] [88, 42.5, 9] [88, 42.5, 13],
 [1, 1, 1] [121.5, 76, 42.5] [117.5, 72, 42.5],
 [1, 1, 1] [167, 121.5, 88] [163, 117.5, 88]]

This is assuming the loop iterates twice, and I am not sure if I have placed the commas or the spacing etc in the right place but ideally I obtain a 3 by 3 matrix with each element having a list with 3 elements.

My code I have so far for the loop is (Up_xyz,Mid_xyz,Down_xyz outputs in [x,x,x] format) :

for i in range (1,len(PeopleName)):       
  x = np.vstack((Up_xyz(TempName[i]),Mid_xyz(TempName[i]),Down_xyz(TempName[i])))
restA.append(x)
l+=1

Which results in:

   [array([[  88. ,   42.5,   13. ],
   [ 117.5,   72. ,   42.5],
   [ 163. ,  117.5,   88. ]])]

Which is simply the value from the last iteration of the loop.

Also, when I append y to restA with

print(y.append(restA))

I get this error:

'numpy.ndarray' object has no attribute 'append'

I assume this is due to the difference in sizing. But I would appreciate any help, and I am fairly new to Python, so would be open to any other ways that would be more efficient as well. Thanks

2 Answers
Related