Home > other >  How to append an array to an empty matrix from for loop by using python numpy or list?
How to append an array to an empty matrix from for loop by using python numpy or list?

Time:07-19

I would like to run this code in order to append an array from each iteration to an empty matrix.

    import numpy as np
    Q = np.array([[10,20,3]])
    storage_Q = np.empty((6,3), dtype = object)
    TotalK = 6 

    for k in range(TotalK):
    Q = Q   [[2,0,1]]
    storage_Q = np.append(Q,Q, axis = 0)

    storage_Q

    I got a result:

    array([[22, 20,  9],
          [22, 20,  9]])

####

What I expect the output is:
####
    storage_Q_ = [[12,20,4], 
                 [14,20,5],
                 [16,20,6], 
                 [18,20,7],
                 [20,20,8],
                 [22,20,9]]

Can anyone suggest me how to revise the code, please? Thank you in advance :)

CodePudding user response:

#input
Q = np.array([[10,20,3]])
increment = np.array([[2,0,1]])
TotalK = 6 

increment_matrix = increment*np.arange(1, TotalK 1).reshape(TotalK,1)
storage_Q = increment_matrix Q

#output
storage_Q

array([[12, 20,  4],
       [14, 20,  5],
       [16, 20,  6],
       [18, 20,  7],
       [20, 20,  8],
       [22, 20,  9]])

Let me know if you understand the procedure or if you have any doubt.

CodePudding user response:

Look at your code step by step:

In [281]:    Q = np.array([[10,20,3]])
     ...:    storage_Q = np.empty((6,3), dtype = object)
     ...:    TotalK = 6

In [282]: storage_Q
Out[282]: 
array([[None, None, None],
       [None, None, None],
       [None, None, None],
       [None, None, None],
       [None, None, None],
       [None, None, None]], dtype=object)

Now do on loop step:

In [283]: k=0    
In [284]: Q = Q   [[2,0,1]]
     ...: storage_Q = np.append(Q,Q, axis = 0)    
In [285]: Q
Out[285]: array([[12, 20,  4]])
In [286]: storage_Q
Out[286]: 
array([[12, 20,  4],
       [12, 20,  4]])

You changed Q, and then joined it to itself with np.append, and assigned that to storage_Q. You threw away the initial definition (the object dtype).

Next loop will be more of the same, a new Q value, and new storage_Q.

Replace that with a list append, which you should understand better (?)

In [287]: Q = np.array([[10,20,3]])
     ...: storage_Q = []
In [288]: for k in range(6):
     ...:     Q = Q np.array([[2,0,1]])
     ...:     storage_Q.append(Q)
     ...:     

In [289]: storage_Q
Out[289]: 
[array([[12, 20,  4]]),
 array([[14, 20,  5]]),
 array([[16, 20,  6]]),
 array([[18, 20,  7]]),
 array([[20, 20,  8]]),
 array([[22, 20,  9]])]

And combine them:

In [290]: np.vstack(storage_Q)
Out[290]: 
array([[12, 20,  4],
       [14, 20,  5],
       [16, 20,  6],
       [18, 20,  7],
       [20, 20,  8],
       [22, 20,  9]])

Yes, it is possible to do this with a whole-array method, but I suspect you need more practice with basic Python loops - and debugging.

Here's a loop using the (6,3) initial array:

In [298]: Q = np.array([10,20,3])
     ...: storage_Q = np.zeros((6,3),int)  # np.empty ok here
     ...: for i in range(6):
     ...:     Q = Q [2,0,1]
     ...:     storage_Q[i,:] = Q   # assign to a row, not replace
     ...:     

In [299]: storage_Q
Out[299]: 
array([[12, 20,  4],
       [14, 20,  5],
       [16, 20,  6],
       [18, 20,  7],
       [20, 20,  8],
       [22, 20,  9]])
  • Related