Convert a Matrix into another Matrix of given dimensions | Function to construct a matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Print the resultant matrix ; Driver Code
def ConstMatrix ( mat , N , M , A , B ) : NEW_LINE INDENT if ( N * M != A * B ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT idx = 0 ; NEW_LINE res = [ [ 0 for i in range ( B ) ] for i in range ( A ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT res [ idx // B ] [ idx % B ] = mat [ i ] [ j ] ; NEW_LINE idx += 1 NEW_LINE DEDENT DEDENT for i in range ( A ) : NEW_LINE INDENT for j in range ( B ) : NEW_LINE INDENT print ( res [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] ] NEW_LINE A = 2 NEW_LINE B = 3 NEW_LINE N = len ( mat ) NEW_LINE M = len ( mat [ 0 ] ) NEW_LINE ConstMatrix ( mat , N , M , A , B ) NEW_LINE DEDENT