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;
Asked By: Kicsi Mano

||

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.

Answered By: lvc

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:

Link

The only very important thing is that you begin your python code with:

import numpy as np
Answered By: Fabrizio
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.