How to split a matrix into 4 blocks using numpy?

后端 未结 3 1394
一向
一向 2020-12-16 16:35

I\'m implementing Strassen\'s Matrix Multiplication using python. In divide step, we divide a larger matrix into smaller sub-matrices. Is there a built-in numpy function to

3条回答
  •  时光取名叫无心
    2020-12-16 17:13

    I ran into the same problem and found some built-in numpy functions to split my matrix into 4 submatrices (my matrices are of size 2^N*2^N)

    Here is the code i wrote:

    upper_half = np.hsplit(np.vsplit(my_matrix, 2)[0], 2)
    lower_half = np.hsplit(np.vsplit(my_matrix, 2)[1], 2)
    
    upper_left = upper_half[0]
    upper_right = upper_half[1]
    lower_left = lower_half[0]
    lower_right = lower_half[1]
    

    Bonus to recombine them using numpy:

    C=np.vstack([np.hstack([c11, c12]), np.hstack([c21, c22])])
    

    vsplit hsplit hstack and vstack seem to be made for that purpose

提交回复
热议问题