pythonmatlabnumpyporting

Translate Matlab code to Numpy


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;

Solution

  • 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.