There are times when I have to port a MATLAB program to Python about once a year, but I will summarize the tips and precautions I noticed at that time. Will be added at any time. (The following includes NumPy / SciPy official documentation on differences from MATLAB And Numpy for MATLAB users contains many duplicates.)
The transpose of the matrix M is M'in MATLAB and M.T in NumPy.
The operation [A B] on arrays A, B in MATLAB is equivalent to numpy.hstack ([A, B]) in NumPy. (A and B must be the same size except in the last dimension.)
size() The size of each dimension of array A is calculated by size (A) in MATLAB, but by numpy.shape (A) in NumPy. Note that numpy.size (A) will result in the number of elements contained in array A.
Everyone should be careful when porting that the index of an array element starts from 1 in MATLAB and 0 in NumPy, but there is also a difference in the index for the ** dimension ** of the array. You need to be careful. That is, sum (A, 2) in MATLAB sums along the second dimension of array A, which must be numpy.sum (A, axis = 1) in NumPy.
Sum (A) in MATLAB sums along the first dimension of array A, while NumPy sums all the elements of array A (unless you specify axis). Therefore, ** sum (A) in MATLAB is equivalent to numpy.sum (A, axis = 0) in NumPy **. This behavior is the same for other functions that perform operations along a specific dimension, such as mean (), std (), var (), max (), min ().
In relation to the above, in MATLAB and NumPy about what dimension the resulting array has when performing operations along a specific dimension with sum (), mean (), std (), var (), etc. There are differences and you need to be careful. For example, if you want to calculate "take an average along the second dimension of a three-dimensional array A and subtract it from the original array", you can find it with A --mean (A, 2) in MATLAB, but with NumPy. A --I get an error when I try numpy.mean (A, 1). This is ** the result of mean (A, 2) in MATLAB is a 3D array (the size of the averaged dimension is 1), whereas the result of numpy.mean (A, 1) in NumPy This is because the result is a two-dimensional array (the averaged dimension is erased) **, so subtraction from the three-dimensional array is not possible (generally). In this case, in NumPy, it is necessary to explicitly supplement the erased dimension with numpy.newaxis as A --numpy.mean (A, 1) [:, numpy.newaxis,:].
reshape() If you want to change only the shape without changing the elements of the array, use reshape (). For example, when you want to rearrange the array A as [1, 2, 3, 4] into a 2x2 2D array, reshape (in MATLAB). A, 2, 2) produces [1 3; 2 4], while NumPy produces numpy.reshape (A, (2, 2)) [[1, 2], [3, 4]. ]] Is generated. In other words, ** MATLAB fills elements along the first dimension, but NumPy first fills elements along the last dimension **. If you want NumPy to fill the elements in a MATLAB-like order, you need to give'F'to the keyword argument order in numpy.reshape ().
Related to the above, the operation A (:) on a multidimensional array A in MATLAB returns a column vector that rearranges all the elements of A into one dimension, in which ** MATLAB starts from the first element. First, take out the elements and arrange them according to the first dimension **. On the other hand, NumPy's numpy.ravel (A) also rearranges the multidimensional array into a one-dimensional array, but ** NumPy first extracts the elements from the first element along the last dimension and arranges them. ** Therefore, the order of the elements is different from the result of A (:) in MATLAB. If you want NumPy to arrange the elements in a MATLAB-like order, you need to give'F'to the keyword argument order in numpy.ravel ().
zeros(), ones() Zeros (n) in MATLAB produces a two-dimensional array of size n x n and all elements have zero values, while NumPy's numpy.zeros (n) produces a one-dimensional array of size n. So ** zeros (n) in MATLAB is equivalent to numpy.zeros ((n, n)) in NumPy ** (note that you need to tuple the shape of the generated array). ). If there are two or more arguments, both MATLAB and NumPy behave in the same way. That is, both zeros (m, n) in MATLAB and numpy.zeros ((m, n)) in NumPy produce a two-dimensional array of size m x n. This behavior is the same for ones ().
rank() In MATLAB, the rank of the matrix M is calculated by rank (M), but in NumPy, it is calculated by numpy.linalg.matrix_rank (M). Note that numpy.rank (M) results in the number of dimensions of the array M (that is, M.ndim, that is, len (M.shape)).
eig() The eigenvalues of the matrix M are found by eig (M) in MATLAB, but by numpy.linalg.eigvals (M) in NumPy. Note that if you set numpy.eig (M), in addition to the vector that has the eigenvalue as the element, you will also receive the matrix that has the eigenvector as the column at the same time (w, V = numpy.eig (M), the vector w is the eigenvalue, Matrix V is the eigenvector). Even in MATLAB, if you receive two return values from eig (M), you can get the eigenvalue and the eigenvector. In this case, the matrix that has the eigenvector in the column and the diagonal ** matrix that has the eigenvalue in the diagonal component ** Note that it will be received in the form of ([V, D] = eig (M), matrix V is the eigenvector and matrix D is the eigenvalue).
svd() For the singular value decomposition of the matrix M, MATLAB uses svd (M) and NumPy uses numpy.linalg.svd (M). If you make MATLAB receive three return values, you will get matrices U, S, and V, where S is a matrix with singular values as diagonal elements and U and V are matrices with left and right singular vectors in columns, respectively. .. Arrays u, d, v are obtained from NumPy's svd (M), where u is a matrix with left singular vectors in columns, d is a ** one-dimensional array with singular values as elements **, and v is a right singular vector. Is a ** matrix with ** rows. In other words, U in MATLAB and u in NumPy match, but the others do not, and S corresponds to numpy.diag (d) and V corresponds to the transpose of v (Hermitian transpose in the case of a complex matrix). ..
cov() A function that calculates the covariance between variables from a multivariable data matrix, but in MATLAB and NumPy the rows and columns of the data matrix are interpreted in reverse. That is, in MATLAB, each ** column ** corresponds to each variable, but in NumPy, each ** row ** corresponds to each variable. Therefore, MATLAB cov (X) is equivalent to NumPy's numpy.cov (X.T).
linspace(a, b, n) It behaves almost the same in MATLAB and NumPy, with the difference that when n is 1, MATLAB returns b, but NumPy returns a.
There are several ways to represent a MATLAB structure in Python, but I think it is often convenient to use a NumPy structured array. Note, however, that MATLAB structs can add fields dynamically, while NumPy's structured array must define all fields when declaring.
The operation [S.f] for the field f of the structure S in MATLAB corresponds to S ['f'] for the field f of the structured array S of NumPy. However, if S ['f'] is a multidimensional array, then it should be numpy.hstack (S ['f']) (that is, the array is decomposed for the first dimension and in the direction of the last dimension. Rejoin).
Recommended Posts