Matlab to Python代码代码迁移
Matlab to Python code code migration
我正在尝试使用 Numpy 包将以下 MATLAB 代码转换为 Python:
c=3e8;
h=6.625e-34;
v=c/1.5;
pulse_w = 1*1e-6;
rep_rate = 25e3;
time_per = 1/rep_rate;
YDFA_L=12;
data = dlmread('test.csv');
ASE_lamda1=1000e-9;
ASE_lamda2=1100e-9;
del_lamda= 2e-9;
ASE_lamda = (ASE_lamda1:del_lamda: ASE_lamda2)';
当我在 OCTAVE 中看到 ASE_lamda 变量的详细信息时,我得到以下输出:
ASE_lamda =
1.0000e-06
1.0020e-06
1.0040e-06
1.0060e-06
1.0080e-06
1.0100e-06
1.0120e-06
1.0140e-06
1.0160e-06
1.0180e-06
1.0200e-06
1.0220e-06
1.0240e-06
1.0260e-06
1.0280e-06
1.0300e-06
1.0320e-06
1.0340e-06
1.0360e-06
1.0380e-06
1.0400e-06
1.0420e-06
1.0440e-06
1.0460e-06
1.0480e-06
1.0500e-06
1.0520e-06
1.0540e-06
1.0560e-06
1.0580e-06
1.0600e-06
1.0620e-06
1.0640e-06
1.0660e-06
1.0680e-06
1.0700e-06
1.0720e-06
1.0740e-06
1.0760e-06
1.0780e-06
1.0800e-06
1.0820e-06
1.0840e-06
1.0860e-06
1.0880e-06
1.0900e-06
1.0920e-06
1.0940e-06
1.0960e-06
1.0980e-06
1.1000e-06
octave:19> whos ASE_lamda
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ASE_lamda 51x1 408 double
Total is 51 elements using 408 bytes
下面是我的eqv Python代码:
c = 3e8
h = 6.625e-34
v = c / 1.5
pulse_w = 1.*1e-6
rep_rate = 25e3
time_per = 1./rep_rate
YDFA_L = 12.
data = np.genfromtxt('test.csv', delimiter=',')
ASE_lamda1 = 1000e-9
ASE_lamda2 = 1100e-9
del_lamda = 2e-9
ASE_lamda = np.arange(ASE_lamda1, (ASE_lamda2)+(del_lamda), del_lamda).conj().T
print ASE_lamda,ASE_lamda.shape, type(ASE_lamda)
Python 代码打印出以下输出:
[ 1.00000000e-06 1.00200000e-06 1.00400000e-06 1.00600000e-06
1.00800000e-06 1.01000000e-06 1.01200000e-06 1.01400000e-06
1.01600000e-06 1.01800000e-06 1.02000000e-06 1.02200000e-06
1.02400000e-06 1.02600000e-06 1.02800000e-06 1.03000000e-06
1.03200000e-06 1.03400000e-06 1.03600000e-06 1.03800000e-06
1.04000000e-06 1.04200000e-06 1.04400000e-06 1.04600000e-06
1.04800000e-06 1.05000000e-06 1.05200000e-06 1.05400000e-06
1.05600000e-06 1.05800000e-06 1.06000000e-06 1.06200000e-06
1.06400000e-06 1.06600000e-06 1.06800000e-06 1.07000000e-06
1.07200000e-06 1.07400000e-06 1.07600000e-06 1.07800000e-06
1.08000000e-06 1.08200000e-06 1.08400000e-06 1.08600000e-06
1.08800000e-06 1.09000000e-06 1.09200000e-06 1.09400000e-06
1.09600000e-06 1.09800000e-06 1.10000000e-06 1.10200000e-06] (52,) <type 'numpy.ndarray'>
为什么 Python 数组有 52 个元素,而 MATLAB / OCTAVE 的数组是 51 个元素 - 据我了解 MATLAB / OCTAVE 数组索引也从 1 而不是 0 开始?
在你对 arange
的调用中,你添加了一个额外的点,就像 Matlab 版本一样:
>>> np.arange(ASE_lamda1, ASE_lamda2, del_lamda).shape
(51,)
我正在尝试使用 Numpy 包将以下 MATLAB 代码转换为 Python:
c=3e8;
h=6.625e-34;
v=c/1.5;
pulse_w = 1*1e-6;
rep_rate = 25e3;
time_per = 1/rep_rate;
YDFA_L=12;
data = dlmread('test.csv');
ASE_lamda1=1000e-9;
ASE_lamda2=1100e-9;
del_lamda= 2e-9;
ASE_lamda = (ASE_lamda1:del_lamda: ASE_lamda2)';
当我在 OCTAVE 中看到 ASE_lamda 变量的详细信息时,我得到以下输出:
ASE_lamda =
1.0000e-06
1.0020e-06
1.0040e-06
1.0060e-06
1.0080e-06
1.0100e-06
1.0120e-06
1.0140e-06
1.0160e-06
1.0180e-06
1.0200e-06
1.0220e-06
1.0240e-06
1.0260e-06
1.0280e-06
1.0300e-06
1.0320e-06
1.0340e-06
1.0360e-06
1.0380e-06
1.0400e-06
1.0420e-06
1.0440e-06
1.0460e-06
1.0480e-06
1.0500e-06
1.0520e-06
1.0540e-06
1.0560e-06
1.0580e-06
1.0600e-06
1.0620e-06
1.0640e-06
1.0660e-06
1.0680e-06
1.0700e-06
1.0720e-06
1.0740e-06
1.0760e-06
1.0780e-06
1.0800e-06
1.0820e-06
1.0840e-06
1.0860e-06
1.0880e-06
1.0900e-06
1.0920e-06
1.0940e-06
1.0960e-06
1.0980e-06
1.1000e-06
octave:19> whos ASE_lamda
Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
ASE_lamda 51x1 408 double
Total is 51 elements using 408 bytes
下面是我的eqv Python代码:
c = 3e8
h = 6.625e-34
v = c / 1.5
pulse_w = 1.*1e-6
rep_rate = 25e3
time_per = 1./rep_rate
YDFA_L = 12.
data = np.genfromtxt('test.csv', delimiter=',')
ASE_lamda1 = 1000e-9
ASE_lamda2 = 1100e-9
del_lamda = 2e-9
ASE_lamda = np.arange(ASE_lamda1, (ASE_lamda2)+(del_lamda), del_lamda).conj().T
print ASE_lamda,ASE_lamda.shape, type(ASE_lamda)
Python 代码打印出以下输出:
[ 1.00000000e-06 1.00200000e-06 1.00400000e-06 1.00600000e-06
1.00800000e-06 1.01000000e-06 1.01200000e-06 1.01400000e-06
1.01600000e-06 1.01800000e-06 1.02000000e-06 1.02200000e-06
1.02400000e-06 1.02600000e-06 1.02800000e-06 1.03000000e-06
1.03200000e-06 1.03400000e-06 1.03600000e-06 1.03800000e-06
1.04000000e-06 1.04200000e-06 1.04400000e-06 1.04600000e-06
1.04800000e-06 1.05000000e-06 1.05200000e-06 1.05400000e-06
1.05600000e-06 1.05800000e-06 1.06000000e-06 1.06200000e-06
1.06400000e-06 1.06600000e-06 1.06800000e-06 1.07000000e-06
1.07200000e-06 1.07400000e-06 1.07600000e-06 1.07800000e-06
1.08000000e-06 1.08200000e-06 1.08400000e-06 1.08600000e-06
1.08800000e-06 1.09000000e-06 1.09200000e-06 1.09400000e-06
1.09600000e-06 1.09800000e-06 1.10000000e-06 1.10200000e-06] (52,) <type 'numpy.ndarray'>
为什么 Python 数组有 52 个元素,而 MATLAB / OCTAVE 的数组是 51 个元素 - 据我了解 MATLAB / OCTAVE 数组索引也从 1 而不是 0 开始?
在你对 arange
的调用中,你添加了一个额外的点,就像 Matlab 版本一样:
>>> np.arange(ASE_lamda1, ASE_lamda2, del_lamda).shape
(51,)