Translate Matlab code to Numpy
Question:
I just started to translate a Matlab code to numpy, how can I write the following code in python
InputVec = [2,3,4]
InputVariable(1,:)=InputVec;
Answers:
According to Numpy for Matlab Users, that code would become:
InputVec = np.array([2, 3, 4])
InputVariable[0,:] = InputVec
The only potentially surprising thing about this is that indices into numpy arrays start at 0, per Python convention, instead of 1 as in Matlab. But, given the table in that link and a reasonable working knowledge of Python, translation from Matlab, at least of small bits of code like that, should be reasonably trivial.
The translation of Matlab code into Python code (with numpy) is generally very easy.
Once you have an idea of the few (maybe 10) little syntactical differences between the two script languages you wont have any problem.
Have a look at the "Linear Algebra Equivalents" section of this page:
The only very important thing is that you begin your python code with:
import numpy as np
I just started to translate a Matlab code to numpy, how can I write the following code in python
InputVec = [2,3,4]
InputVariable(1,:)=InputVec;
According to Numpy for Matlab Users, that code would become:
InputVec = np.array([2, 3, 4])
InputVariable[0,:] = InputVec
The only potentially surprising thing about this is that indices into numpy arrays start at 0, per Python convention, instead of 1 as in Matlab. But, given the table in that link and a reasonable working knowledge of Python, translation from Matlab, at least of small bits of code like that, should be reasonably trivial.
The translation of Matlab code into Python code (with numpy) is generally very easy.
Once you have an idea of the few (maybe 10) little syntactical differences between the two script languages you wont have any problem.
Have a look at the "Linear Algebra Equivalents" section of this page:
The only very important thing is that you begin your python code with:
import numpy as np