Adding a matrix in Python in a loop

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

Then I 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 that I get:

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

So what I would like to achieve is basically

 [[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 assumes the loop repeats twice, and I'm not sure if I put commas or spacing, etc. in the right place, but ideally I get a 3 by 3 matrix, each element has a list of 3 elements.

My code that I still have for the loop (Up_xyz, Mid_xyz, Down_xyz prints in the format [x, x, x]):

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

Result:

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

This is just the value from the last iteration of the loop.

Also, when I add y for restA with

print(y.append(restA))

I get this error:

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

, . , Python, , .

+4
2

np.append, ( ). . :

arr , . , append : . None, out - .

( 3 , , , )

, 3 , :

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)

:

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)

list np.array :

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)

:

[[[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]]]

2D-, 1D-.

3D- (3,3,3) :

np.stack([a,b,c])
+3

for

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 . , :

y = np.append(y, restA)
+1

Source: https://habr.com/ru/post/1680672/


All Articles