how to append a numpy matrix into an empty numpy array

前端 未结 4 1326

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:37

    It may not be the best solution but it seems to work.

    data = np.array([[2, 2, 2], [3, 3, 3]])
    Weights = np.array([[4, 4, 4], [4, 4, 4], [4, 4, 4]])
    All = []
    
    for i in data:
        for j in Weights:
            h = i * j
            All.append(h)
    
    All = np.array(All)
    

    I'd like to say it's not the best solution because it appends the result to a list and at the end converts the list in a numpy array but it works good for small applications. I mean if you have to do heavy calculations like this it's i would consider finding another method. Anyway with this method you don't have to think about the number conversions from floating point. Hope this helps.

提交回复
热议问题