First row of numpy.ones is still populated after referencing another matrix

前端 未结 5 968
说谎
说谎 2021-01-26 10:01

I have a matrix \'A\' whose values are shown below. After creating a matrix \'B\' of ones using numpy.ones and assigning the values from \'A\' to \'B\' by indexing \'i\' rows an

5条回答
  •  不知归路
    2021-01-26 11:02

    A cleaner way, in my opinion, of writing the C loop is:

    for k in range(1,7):
        for l in range(1,7):
            if B[k,l]==8:
                C[k-1, l-1] += D[k,l]
    

    That inner block of B (and D) can be selected with slices, B[1:7, 1:7] or B[1:-1, 1:-1].

    A and D are defined as np.matrix. Since we aren't doing matrix multiplications here (no dot products), that can create problems. For example I was puzzled why

    In [27]: (B[1:-1,1:-1]==8)*D[1:-1,1:-1]
    Out[27]: 
    matrix([[2, 1, 2, 3, 3, 3],
            [3, 3, 3, 4, 5, 5],
            [1, 2, 1, 1, 2, 2],
            [2, 2, 3, 2, 3, 1],
            [2, 2, 3, 2, 3, 1],
            [2, 3, 3, 2, 3, 2]])
    

    What I expected (and matches the loop C) is:

    In [28]: (B[1:-1,1:-1]==8)*D.A[1:-1,1:-1]
    Out[28]: 
    array([[2, 0, 0, 0, 0, 0],
           [1, 2, 0, 0, 0, 0],
           [0, 3, 0, 0, 0, 0],
           [0, 0, 0, 2, 0, 0],
           [0, 0, 0, 2, 0, 0],
           [0, 0, 0, 0, 3, 0]])
    

    B = A.copy() still leaves B as matrix. B=A.A returns an np.ndarray. (as does np.copy(A))

    D.A is the array equivalent of D. B[1:-1,1:-1]==8 is boolean, but when used in the multiplication context it is effectively 0s and 1s.

    But if we want to stick with np.matrix then I'd suggest using the element by element multiply function:

    In [46]: np.multiply((A[1:-1,1:-1]==8), D[1:-1,1:-1])
    Out[46]: 
    matrix([[2, 0, 0, 0, 0, 0],
            [1, 2, 0, 0, 0, 0],
            [0, 3, 0, 0, 0, 0],
            [0, 0, 0, 2, 0, 0],
            [0, 0, 0, 2, 0, 0],
            [0, 0, 0, 0, 3, 0]])
    

    or just multiply the full matrixes, and select the inner block after:

    In [47]: np.multiply((A==8), D)[1:-1, 1:-1]
    Out[47]: 
    matrix([[2, 0, 0, 0, 0, 0],
            [1, 2, 0, 0, 0, 0],
            [0, 3, 0, 0, 0, 0],
            [0, 0, 0, 2, 0, 0],
            [0, 0, 0, 2, 0, 0],
            [0, 0, 0, 0, 3, 0]])
    

提交回复
热议问题