Python: Resize an existing array and fill with zeros

后端 未结 5 1391
遥遥无期
遥遥无期 2020-12-30 20:55

I think that my issue should be really simple, yet I can not find any help on the Internet whatsoever. I am very new to Python, so it is possible that I am missing somethin

5条回答
  •  感动是毒
    2020-12-30 21:23

    This solution works with resize function

    Take a sample array

    S= np.ones((3))
    print (S)
    # [ 1.  1.  1.]
    d= np.diag(S) 
    print(d)
    """
    [[ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]]
    
    """
    

    This dosent work, it just add a repeating values

    np.resize(d,(6,3))
    """
    adds a repeating value
    array([[ 1.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  1.],
           [ 1.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  1.]])
    """
    

    This does work

    d.resize((6,3),refcheck=False)
    print(d)
    """
    [[ 1.  0.  0.]
     [ 0.  1.  0.]
     [ 0.  0.  1.]
     [ 0.  0.  0.]
     [ 0.  0.  0.]
     [ 0.  0.  0.]]
    """
    

提交回复
热议问题