Programmer
Programmer

Reputation: 8707

Matlab to Python code code migration

I was trying t convert the below MATLAB code to Python using Numpy package:

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)';

When I see the details of ASE_lamda variable in OCTAVE I get the below output:

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

Below is my eqv Python Code:

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)

The Python code prints out the below output:

[  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'>

Why is the Python array of 52 elements while that of MATLAB / OCTAVE is 51 elements - as far as I understand MATLAB / OCTAVE array indexes also starts from 1 rather than 0?

Upvotes: 0

Views: 633

Answers (1)

elyase
elyase

Reputation: 40973

In your call to arange, your adding an additional point, do it exactly like the Matlab version:

>>> np.arange(ASE_lamda1, ASE_lamda2, del_lamda).shape
(51,)

Upvotes: 2

Related Questions