C++ Function to Reshape the Matrix


In MATLAB, there is a very useful function called ‘reshape’, which can reshape a matrix into a new one with different size but keep its original data. You’re given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to 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.

Example:
Input: nums =
[[1,2],
[3,4]]
r = 1, c = 4
Output:
[[1,2,3,4]]

The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

Another Example:
Input: nums =
[[1,2],
[3,4]]
r = 2, c = 4
Output:
[[1,2],
[3,4]]

There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

Reshaping a two dimension matrix in C++ follows two steps: the first is to create a result matrix, the second step is to walk through the original array in the row-to-row method, and fill the element sequentially in the result array. The following methods are roughly the same but differ in the way how the elements are accessed.

Array Index

Using two array indices i and j pointing to the target position for elements to write to the new array. Rewind the column index to 0 and Increment the row number when the cursor hits the end of a row:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        vector<vector<int>> result(r, vector<int>(c));
        int i = 0, j = 0;
        for (int row = 0; row < x; ++ row) {
            for (int col = 0; col < y; ++ col) {
                result[i][j ++] = nums[row][col];
                if (j >= c) {
                    i ++;
                    j = 0;
                }
            }
        }
        return result;
    }
};
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        vector<vector<int>> result(r, vector<int>(c));
        int i = 0, j = 0;
        for (int row = 0; row < x; ++ row) {
            for (int col = 0; col < y; ++ col) {
                result[i][j ++] = nums[row][col];
                if (j >= c) {
                    i ++;
                    j = 0;
                }
            }
        }
        return result;
    }
};

Using Counter

When we iterate the original matrix, we have a counter to denote the number of elements visited so far, and then we can compute the position in the new matrix:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        vector<vector<int>> result(r, vector<int>(c));
        int cnt = 0;
        for (int row = 0; row < x; ++ row) {
            for (int col = 0; col < y; ++ col) {                
                result[cnt / c][cnt % c] = nums[row][col];
                cnt ++;
            }
        }
        return result;
    }
};
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        vector<vector<int>> result(r, vector<int>(c));
        int cnt = 0;
        for (int row = 0; row < x; ++ row) {
            for (int col = 0; col < y; ++ col) {                
                result[cnt / c][cnt % c] = nums[row][col];
                cnt ++;
            }
        }
        return result;
    }
};

The counter value can also be computed from the row and column.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        vector<vector<int>> result(r, vector<int>(c));
        for (int row = 0; row < x; ++ row) {
            for (int col = 0; col < y; ++ col) {                
                int cnt = row * y + col;
                result[cnt / c][cnt % c] = nums[row][col];
            }
        }
        return result;
    }
};
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        vector<vector<int>> result(r, vector<int>(c));
        for (int row = 0; row < x; ++ row) {
            for (int col = 0; col < y; ++ col) {                
                int cnt = row * y + col;
                result[cnt / c][cnt % c] = nums[row][col];
            }
        }
        return result;
    }
};

Using Queue

We can also do this by storing the elements in the queue. And then pop all elements in the queue one by one – fill it in the new matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        queue<int> data;
        for (const auto &n: nums) {
            for (const auto &m: n) {
                data.push(m);
            }
        }
        vector<vector<int>> result(r, vector<int>(c));
        for (int row = 0; row < r; ++ row) {
            for (int col = 0; col < c; ++ col) {                
                result[row][col] = data.front();
                data.pop();
            }
        }        
        return result;
    }
};
class Solution {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int x = nums.size();
        int y = nums[0].size();
        if (x * y != r * c) {
            return nums;
        }
        queue<int> data;
        for (const auto &n: nums) {
            for (const auto &m: n) {
                data.push(m);
            }
        }
        vector<vector<int>> result(r, vector<int>(c));
        for (int row = 0; row < r; ++ row) {
            for (int col = 0; col < c; ++ col) {                
                result[row][col] = data.front();
                data.pop();
            }
        }        
        return result;
    }
};

See also: GoLang: Reshape the Matrix

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
793 words
Last Post: Compute the Maximum Product of Three Numbers in an Array
Next Post: Tips For Keeping Your VPS Flawless

The Permanent URL is: C++ Function to Reshape the Matrix

Leave a Reply