Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Appending a Matrix in Python within loop

Tags:

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

like image 983
Questions Avatar asked Jul 03 '17 21:07

Questions


2 Answers

It exists np.append, but it is very costly in a loop (if you append one by one). See documentation:

A copy of arr with values appended to axis. Note that append does not occur in-place: a new array is allocated and filled. If axis is None, out is a flattened array.

A copy the array is done for each increment of your loop (here it is only 3 increments but I think it is not a good practice to do that, be aware)

Ok you have 3 arrays, and you want to merge each one:

import numpy as np

a = np.array([[ 1.,  1.,  1.],
              [ 1.,  1.,  1.],
              [ 1.,  1.,  1.]])

b = np.array([[  88.,    42.5,    9. ],
              [ 121.5,   76.,    42.5],
              [ 167.,  121.5,   88. ]])

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

result = np.empty((3,3), dtype=object)

n, p = result.shape
for i in range(n):
      result[i, 0] = a[i,:]
      result[i, 1] = b[i,:]
      result[i, 2] = c[i,:]

print(result)

Output:

array([[array([ 1.,  1.,  1.]), array([ 88. ,  42.5,   9. ]), 
       array([ 88. ,  42.5,  13. ])],
       [array([ 1.,  1.,  1.]), array([ 121.5,   76. ,   42.5]),
        array([ 117.5,   72. ,   42.5])],
       [array([ 1.,  1.,  1.]), array([ 167. ,  121.5,   88. ]),
        array([ 163. ,  117.5,   88. ])]], dtype=object)

If you want list instead of np.array do:

n, p = result.shape
for i in range(n):
    result[i, 0] = a[i,:].tolist()
    result[i, 1] = b[i,:].tolist()
    result[i, 2] = c[i,:].tolist()

print(result)

Output:

[[[1.0, 1.0, 1.0] [88.0, 42.5, 9.0] [88.0, 42.5, 13.0]]
 [[1.0, 1.0, 1.0] [121.5, 76.0, 42.5] [117.5, 72.0, 42.5]]
 [[1.0, 1.0, 1.0] [167.0, 121.5, 88.0] [163.0, 117.5, 88.0]]]

It is a bit strange to have a 2D array where each element is a 1D array.

You can directly have a 3D array (3,3,3) shape with :

np.stack([a,b,c])
like image 181
glegoux Avatar answered Oct 20 '22 12:10

glegoux


You should append within the for loop

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

Numpy array object does not have a method append. You might want:

y = np.append(y, restA)
like image 21
Luckk Avatar answered Oct 20 '22 13:10

Luckk