文章目录
- Matlab、python函数对应表
- MATLAB
Matlab | Python |
numel(X) | X.size |
size(X, 2) | X.shape[1] |
A.*B | A*B |
A*B | A.dot(B) |
X’ | X.conj().T |
| X[0:5, :] |
X(1:2, 4:7) | X[0:2,3:7] |
repmat(X, 2, 3) | np.tile(X, (2, 3)) |
[a b] or [a, b] | np.hstack((a,b)) |
[a; b] | np.vstack((a,b)) |
ones(3,4) | np.ones((3, 4)) |
Matlab、python函数对应表
Matlab2python:http://ompclib.appspot.com/m2py
numpy.array | numpy.matrix | Notes | |
| | 获取a(张量/秩)的维数 | |
| | 获取数组元素的数目 | |
| | get the “size” of the matrix | |
| | get the number of elements of the nth dimension of array a. (Note that MATLAB® uses 1 based indexing while Python uses 0 based indexing, See note ‘INDEXING’) | |
| | | 2x3 matrix literal |
| | | construct a matrix from blocks a,b,c, and d |
| | | access last element in the 1xn matrix |
| | access element in second row, fifth column | |
| | entire second row of | |
| | the first five rows of | |
| | the last five rows of | |
| | rows one to three and columns five to nine of | |
| | rows 2,4 and 5 and columns 1 and 3. This allows the matrix to be modified, and doesn’t require a regular slice. | |
| | every other row of | |
| | every other row of | |
| | | |
| | | |
| | transpose of | |
| | | conjugate transpose of |
| | | matrix multiply |
| | | element-wise multiply |
| | element-wise divide | |
| | | element-wise exponentiation |
| | matrix whose i,jth element is (a_ij > 0.5) | |
| | find the indices where (a > 0.5) | |
| | | extract the columms of a where vector v > 0.5 |
| | | extract the columms of a where column vector v > 0.5 |
| | a with elements less than 0.5 zeroed out | |
| | | a with elements less than 0.5 zeroed out |
| | set all values to the same scalar value | |
| | numpy assigns by reference | |
| | numpy slices are by reference | |
| | turn array into vector (note that this forces a copy) | |
| | | create an increasing vector see note ‘RANGES’ |
| | | create an increasing vector see note ‘RANGES’ |
| | | create a column vector |
| | | 3x4 rank-2 array full of 64-bit floating point zeros |
| | | 3x4x5 rank-3 array full of 64-bit floating point zeros |
| | | 3x4 rank-2 array full of 64-bit floating point ones |
| | | 3x3 identity matrix |
| | | vector of diagonal elements of a |
| | | square diagonal matrix whose nonzero values are the elements of a |
| | | random 3x4 matrix |
| | | 4 equally spaced samples between 1 and 3, inclusive |
| | | two 2D arrays: one of x values, the other of y values |
| | the best way to eval functions on a grid | |
| | | |
| | the best way to eval functions on a grid | |
| | | create m by n copies of a |
| | | concatenate columns of |
| | | concatenate rows of a and b |
| | maximum element of a (with ndims(a)<=2 for matlab) | |
| | maximum element of each column of matrix a | |
| | maximum element of each row of matrix a | |
| | compares a and b element-wise, and returns the maximum value from each pair | |
| | | L2 norm of vector v |
| | element-by-element AND operator (Numpy ufunc) see note ‘LOGICOPS’ | |
| | element-by-element OR operator (Numpy ufunc) see note ‘LOGICOPS’ | |
| | bitwise AND operator (Python native and Numpy ufunc) | |
| | bitwise OR operator (Python native and Numpy ufunc) | |
| | inverse of square matrix a | |
| | pseudo-inverse of matrix a | |
| | rank of a matrix a | |
| | solution of a x = b for x | |
| Solve a.T x.T = b.T instead | solution of x a = b for x | |
| | singular value decomposition of a | |
| | cholesky factorization of a matrix (chol(a) in matlab returns an upper triangular matrix, but linalg.cholesky(a) returns a lower triangular matrix) | |
| | eigenvalues and eigenvectors of a | |
| | eigenvalues and eigenvectors of a,b | |
| find the k largest eigenvalues and eigenvectors of a | ||
| | | QR decomposition |
| | | LU decomposition (note: P(Matlab) == transpose(P(numpy)) ) |
| | | Conjugate gradients solver |
| | | Fourier transform of a |
| | | inverse Fourier transform of a |
| | | sort the matrix |
| | sort the rows of the matrix | |
| | multilinear regression | |
| | downsample with low-pass filtering | |
| | ||
| |
MATLAB
numpy | Notes | |
| | get help on the function func |
| (See note ‘HELP’) | find out where func is defined |
| | print source for func (if not a native function) |
a && b | a and b | short-circuiting logical AND operator (Python native operator); scalar arguments only |
a || b | a or b | short-circuiting logical OR operator (Python native operator); scalar arguments only |
| | complex numbers |
| | Distance between 1 and the nearest floating point number |
| | integrate an ODE with Runge-Kutta 4,5 |
| | integrate an ODE with BDF |