Python Solutions
Solution 1 - NumPy\n\n\nWhen I read "MATLAB", I immediately thought "NumPy". Thanks to @fallcreek for pointing out tolist, makes converting the result to the co
#### **Solution 1 - `NumPy`**\n\n\nWhen I read "MATLAB", I immediately thought "NumPy". Thanks to @fallcreek for pointing out `tolist`, makes converting the result to the correct type easier than what I had originally.\n```\nimport numpy as np\n\nclass Solution(object):\n    def matrixReshape(self, nums, r, c):\n        try:\n            return np.reshape(nums, (r, c)).tolist()\n        except:\n            return nums\n```\n#### **Solution 2 - Oneliner**\n\nAn ugly oneliner :-)\n\n    def matrixReshape(self, nums, r, c):\n        return nums if len(sum(nums, [])) != r * c else map(list, zip(*([iter(sum(nums, []))]*c)))\n\nA more readable version of that:\n\n    def matrixReshape(self, nums, r, c):\n        flat = sum(nums, [])\n        if len(flat) != r * c:\n            return nums\n        tuples = zip(*([iter(flat)] * c))\n        return map(list, tuples)\n\n#### **Solution 3 - `itertools`**\n\n    def matrixReshape(self, nums, r, c):\n        if r * c != len(nums) * len(nums[0]):\n            return nums\n        it = itertools.chain(*nums)\n        return [list(itertools.islice(it, c)) for _ in xrange(r)]
[]