Learning 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.
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).
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
Reposted to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Important Update of Delegation Service!
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
