Reputation: 8727
I am trying to convert a MATLAB code into Python.
My MATLAB Code:
ASE_lamda1=1000e-9;
ASE_lamda2=1100e-9;
del_lamda= 2e-9;
ASE_lamda = (ASE_lamda1:del_lamda: ASE_lamda2)';
Below is what I am trying as eqv. Python code:
#!/usr/bin/python
import numpy as np
ASE_lamda1 = 9.9999999999999995e-07
ASE_lamda2 = 1100e-9
del_lamda = 2e-9
ASE_lamda = np.transpose(np.arange[ASE_lamda1:del_lamda:ASE_lamda2])
But I am getting the below error:
Traceback (most recent call last):
File "tasks.py", line 22, in <module>
ASE_lamda = np.transpose(np.arange[ASE_lamda1:del_lamda:ASE_lamda2])
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
I am not sure of this error - what it means as I do not have much expertise over Python / Numpy / Scipy.
Upvotes: 3
Views: 102
Reputation: 500933
The
np.arange[ASE_lamda1:del_lamda:ASE_lamda2]
should be
np.arange(ASE_lamda1, ASE_lamda2, del_lamda)
This returns
array([ 1.00000000e-06, 1.00200000e-06, 1.00400000e-06,
1.00600000e-06, 1.00800000e-06, 1.01000000e-06,
...
1.09000000e-06, 1.09200000e-06, 1.09400000e-06,
1.09600000e-06, 1.09800000e-06, 1.10000000e-06])
This is a 1D array, so transposing it is a no-op. You may or may not need to reshape it to 2D depending on what you're going to do with it. An easy way reshape the array to 2D is using slicing and numpy.newaxis
:
In [54]: ASE_lamda[:, np.newaxis]
Out[54]:
array([[ 1.00000000e-06],
[ 1.00200000e-06],
...
[ 1.09800000e-06],
[ 1.10000000e-06]])
In [55]: ASE_lamda[np.newaxis, :]
Out[55]:
array([[ 1.00000000e-06, 1.00200000e-06, 1.00400000e-06,
1.00600000e-06, 1.00800000e-06, 1.01000000e-06,
...
1.09000000e-06, 1.09200000e-06, 1.09400000e-06,
1.09600000e-06, 1.09800000e-06, 1.10000000e-06]])
If you're moving to NumPy from MATLAB, take a look at NumPy for Matlab Users.
Upvotes: 2