Convert a Matrix into another Matrix of given dimensions | Function to cona matrix of size A * B from the given matrix elements ; Initialize a new matrix ; Traverse the matrix , mat [ ] [ ] ; Update idx ; Prlet the resultant matrix ; Driver Code
function ConstMatrix ( mat , N , M , A , B ) { if ( N * M != A * B ) return ; let idx = 0 ; let res = new Array ( A ) ; for ( var i = 0 ; i < res . length ; i ++ ) { res [ i ] = new Array ( 2 ) ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { res [ Math . floor ( idx / B ) ] [ idx % B ] = mat [ i ] [ j ] ; idx ++ ; } } for ( let i = 0 ; i < A ; i ++ ) { for ( let j = 0 ; j < B ; j ++ ) document . write ( res [ i ] [ j ] + " " ) ; document . write ( " " ) ; } } let mat = [ [ 1 , 2 , 3 , 4 , 5 , 6 ] ] ; let A = 2 ; let B = 3 ; let N = mat . length ; let M = mat [ 0 ] . length ; ConstMatrix ( mat , N , M , A , B ) ;