GoLang: Reshape the Matrix


In MATLAB, there is a handy function called reshape which can reshape an m x n matrix into a new one with a different size r x c keeping its original data.

You are given an m x n matrix mat and two integers r and c representing the row number and column number of the wanted reshaped matrix.

The reshaped matrix should be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the reshape operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

reshape-matrix GoLang: Reshape the Matrix algorithms math python teaching kids programming youtube video

Hints:
Do you know how 2d matrix is stored in 1d memory? Try to map 2-dimensions into one.
M[i][j]=M[n*i+j] , where n is the number of cols. This is the one way of converting 2-d indices into one 1-d index. Now, how will you convert 1-d index into 2-d indices?
Try to use division and modulus to convert 1-d index into 2-d indices.
M[i] => M[i/n][n%i] Will it result in right mapping? Take some example and check this formula.

GoLang Matrix Reshape Function

The following is a GoLang Implementation of Matrix Reshape Function. First, we iterate over the original matrix and push all those elements into a list/queue/array. And then, we create the new matrix with the new dimensions and go through elements row by row and column by column, and fill them with the elements in the array/queue (first in first out order).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func matrixReshape(mat [][]int, r int, c int) [][]int {
    var rows, cols = len(mat), len(mat[0])
    if rows * cols != r * c {
        return mat
    }
    var q = make([]int, 0)
    for _, i := range(mat) {
        for _, j := range(i) {
            q = append(q, j)
        }
    }
    var idx = 0
    var ans = make([][]int, r)
    for i := 0; i < r; i ++ {
        ans[i] = make([]int, c)
        for j := 0; j < c; j ++ {
            ans[i][j] = q[idx]
            idx ++
        }
    }
    return ans
}
func matrixReshape(mat [][]int, r int, c int) [][]int {
    var rows, cols = len(mat), len(mat[0])
    if rows * cols != r * c {
        return mat
    }
    var q = make([]int, 0)
    for _, i := range(mat) {
        for _, j := range(i) {
            q = append(q, j)
        }
    }
    var idx = 0
    var ans = make([][]int, r)
    for i := 0; i < r; i ++ {
        ans[i] = make([]int, c)
        for j := 0; j < c; j ++ {
            ans[i][j] = q[idx]
            idx ++
        }
    }
    return ans
}

The time and space complexity is O(N) where N is the number of the elements in the matrix. If the number of elements do not match in the old/new matrix, we simply return the input matrix.

See also: C++ Function to Reshape the Matrix

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
503 words
Last Post: Teaching Kids Programming - Flip to Zeros
Next Post: Teaching Kids Programming - Multiples of 3 and 7

The Permanent URL is: GoLang: Reshape the Matrix

Leave a Reply