Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab vs Python: Reshape

Tags:

So I found this:

When converting MATLAB code it might be necessary to first reshape a matrix to a linear sequence, perform some indexing operations and then reshape back. As reshape (usually) produces views onto the same storage, it should be possible to do this fairly efficiently.

Note that the scan order used by reshape in Numpy defaults to the 'C' order, whereas MATLAB uses the Fortran order. If you are simply converting to a linear sequence and back this doesn't matter. But if you are converting reshapes from MATLAB code which relies on the scan order, then this MATLAB code:

z = reshape(x,3,4);

should become

z = x.reshape(3,4,order='F').copy()

in Numpy.

I have a multidimensional 16*2 array called mafs, when I do in MATLAB:

mafs2 = reshape(mafs,[4,4,2]) 

I get something different than when in python I do:

mafs2 = reshape(mafs,(4,4,2))

or even

mafs2 = mafs.reshape((4,4,2),order='F').copy()

Any help on this? Thank you all.

like image 889
JEquihua Avatar asked Aug 09 '12 21:08

JEquihua


People also ask

Why we use reshape in Matlab?

The reshape function changes the size and shape of an array. For example, reshape a 3-by-4 matrix to a 2-by-6 matrix. As long as the number of elements in each shape are the same, you can reshape them into an array with any number of dimensions.

What does reshape (- 1 in Python do?

Flatten an Array with reshape(-1) Calling reshape() with a single argument -1 flattens an array of any dimensions to a 1D array. Again, the reshape() function treats the -1 as an unknown dimension. In other words, the reshape() function calculates the number of elements in the 1D array we are trying to produce.

Is there reshape method in NumPy?

Yes, as long as the elements required for reshaping are equal in both shapes. We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.

Is Matlab better than NumPy?

MATLAB's scripting language was created for linear algebra so the syntax for some array manipulations is more compact than NumPy's. On the other hand, the API for adding GUIs and creating full-fledged applications is more or less an afterthought. NumPy is based on Python, a general-purpose language.


1 Answers

Example:

MATLAB:

>> mafs = [(1:16)' (17:32)'] mafs =      1    17      2    18      3    19      4    20      5    21      6    22      7    23      8    24      9    25     10    26     11    27     12    28     13    29     14    30     15    31     16    32  >> reshape(mafs,[4 4 2]) ans(:,:,1) =      1     5     9    13      2     6    10    14      3     7    11    15      4     8    12    16 ans(:,:,2) =     17    21    25    29     18    22    26    30     19    23    27    31     20    24    28    32 

Python:

>>> import numpy as np >>> mafs = np.c_[np.arange(1,17), np.arange(17,33)] >>> mafs.shape (16, 2) >>> mafs[:,0] array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]) >>> mafs[:,1] array([17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32])  >>> r = np.reshape(mafs, (4,4,2), order="F") >>> r.shape (4, 4, 2) >>> r[:,:,0] array([[ 1,  5,  9, 13],        [ 2,  6, 10, 14],        [ 3,  7, 11, 15],        [ 4,  8, 12, 16]]) >>> r[:,:,1] array([[17, 21, 25, 29],        [18, 22, 26, 30],        [19, 23, 27, 31],        [20, 24, 28, 32]]) 
like image 116
Amro Avatar answered Sep 28 '22 17:09

Amro