how to append a numpy matrix into an empty numpy array

前端 未结 4 1285

I want to append a numpy array(matrix) into an array through a loop

data=[[2 2 2] [3 3 3]]
Weights=[[4 4 4] [4 4 4] [4 4 4]]
All=np.array([])  
for i in data:
           


        
4条回答
  •  深忆病人
    2021-01-26 09:45

    Adam, how about just using a pair of nested loops? I believe this code will do what you want.

    import numpy as np
    data = ([2,2,2],[3,3,3])
    weights = ([4,4,4],[4,4,4],[4,4,4])
    
    output=np.array([])
    for each_array in data:
        for weight in weights:
                each_multiplication = np.multiply(each_array, weight)
                output = np.append(output,each_multiplication)
    
    print output
    

    np.multiply() performs element wise multiplication instead of matrix multiplication. As best as I can understand from your sample input and output, this is what you're trying to accomplish.

提交回复
热议问题