目录
利用SMOP将matlab转换为python
1 下载SMOP
源码:
git clone https://github.com/victorlei/smop.git
2 安装环境
- 确保有python环境(我用的python3)。
- 确保安装了virtualenv,若没有则:
pip install virtualenv
。 - 没有pip或pip3则:
sudo apt-get install python-pip
(python2)
sudo apt-get install python3-pip
(python3)
在git下载的目录下启动virtualenv。
cd smop
virtualenv smop
source bin/activate
如果你用Anaconda安装的python,则直接启动Anaconda也行,不需要virtualenv。
然后build安装依赖文件:
pip install -e .
命令行显示安装成功:
Requirement already satisfied: pyparsing>=2.0.2 in
d:\programfile\anaconda3\lib\site-packages (from
packaging->pytest->smop==0.41b0) (2.4.7) Installing collected
packages: smop Running setup.py develop for smop Successfully
installed smop
3 使用教程
smop foo.m bar.m bzz.m
smop -h
smop | more
4 数据结构
Empty vector, empty string, and empty cellarray
matlab | numpy |
---|---|
> size([]) | >>> matlabarray().shape |
0 0 | (0, 0) |
> size(’’) | >>> char().shape |
0 0 | (0, 0) |
> size({}) | >>> cellarray().shape |
0 0 | (0, 0) |
Scalars are 1x1 matrices
matlab | numpy |
---|---|
> a=17 | >>> a=matlabarray(17) |
> size(a) | >>> a.shape |
1 1 | 1 1 |
Character string literals
Matlab strings inherit their behavior from Matlab numeric arrays. This
includes base-1 indexing, Fortran data order, and some unexpected
features, such as auto-expand on out of bound assignment (Matlab strings
are mutable objects). Unless we know better, Matlab string literals
should be translated to instances of class char
, which inherits from
matlabarray
.
matlab | numpy |
---|---|
> s=‘helloworld’ | >>> s=char(‘helloworld’) |
> size(s) | >>> print size_(s) |
1 10 | (1,10) |
> s(1:5)=‘HELLO’ | >>> s[1:5]=char(‘HELLO’) |
> s | >>> print s |
HELLOworld | HELLOworld |
> resize(s,[2 5]) | >>> print resize_(s,[2,5]) |
HELLO | HELLO |
world | world |
Row vectors
Rows are matrices whose size is [1 N]. When concatenated, rows are
joined along the first dimension, so concatenating two row vectors
of length M and N yields a row vector of length M+N.
matlab | numpy |
---|---|
> s=[1 2 3] | >>> s=matlabarray([1,2,3]) |
> t=[4 5 6] | >>> t=matlabarray([4,5,6]) |
> u=[s t] | >>> print concat([s,t]) |
1 2 3 4 5 6 |
String concatenation
String concatenation is consistent with row vectors concatenation
because string literals are row vectors
matlab | numpy |
---|---|
> s=‘abc’ | >>> s = char(‘abc’) |
> t=‘ABC’ | >>> t = char(‘ABC’) |
> [s t] | >>> print concat([s,t]) |
abcABC | 1 2 3 4 5 6 |
Column vector
matlab | numpy |
---|---|
> a=[1;2;3] | >>> a=matlabarray([[1], [2], [2]]) |
> size(a) | >>> a.shape |
3 1 | (3, 1) |
Cell arrays
Cell arrays subclass matlabarray and inherit the usual matlab
array behaviour – base-1 indexing, Fortran data order, expand on
out-of-bound assignment, etc. Unlike matlabarray, each element of
cellarray holds a python object.
matlab | numpy |
---|---|
> a = { ‘abc’, 123 } | >>> a=cellarray([‘abc’,123]) |
> a{1} | >>> a[1] |
abc | abc |
Cell arrays of strings
In matlab, cellstrings are cell arrays, where each cell contains a
char object. In numpy, class cellstring derives from matlabarray,
and each cell contains a native python string (not a char
instance).
matlab | numpy |
---|---|
> a = { ‘abc’, ‘hello’ } | >>> a=cellstring([‘abc’, ‘hello’]) |
> a{1} | >>> a[1] |
abc | abc |
C and FORTRAN data layout
Matlab matrix elements are ordered in columns-first order, better known
as FORTRAN order. By default, numpy arrays use C layout. Instances of
matlabarray
use FORTRAN layout, except if created empty, in which
case they use C layout.
matlab | numpy |
---|---|
> reshape(1:4,[2 2]) | >>> a=matlabarray([1,2,3,4]) >>> reshape(a, [2,2]) |
1 3 2 4 |
1 3 2 4 |
适用于matlab的numpy
-
在MATLAB®中,基本数据类型是双精度浮点数的多维数组。大多数表达式采用此类数组并返回此类数组。这些数组的2-D实例上的运算被设计为或多或少地像线性代数中的矩阵运算一样起作用。
-
在NumPy中,基本类型是多维array。在包括2D在内的所有维度上对这些数组进行的操作都是按元素进行的操作。一个人需要对线性代数使用特定的函数(尽管对于矩阵乘法,可以@在python 3.5及更高版本中使用运算符)。
-
在MATLAB®中,数组具有按值传递的语义,并且具有惰性的写时复制方案,可以防止在实际需要之前创建副本。切片操作可复制阵列的某些部分。
-
在NumPy中,数组具有按引用传递的语义。切片操作是将视图放入数组中。
线性代数运算函数替换
MATLAB |
NumPy |
Notes |
---|---|---|
|
|
get the number of dimensions of an array |
|
|
get the number of elements of an array |