1

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;
Amro
125k25 gold badges250 silver badges466 bronze badges
asked Jul 2, 2012 at 10:33
3
  • What does the matlab code do? Commented Jul 2, 2012 at 10:38
  • It sets the variable InputVec to the array [2,3,4] and then sets the first row of InputVariable to [2,3,4] as well. Essentially the OP wants to know how to get the effect of the Matlab function subsasgn in numpy. Commented Jul 2, 2012 at 10:41
  • OK, I see. So InputVariable is a np.matrix then? Commented Jul 2, 2012 at 11:02

2 Answers 2

5

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.

Glorfindel
22.8k13 gold badges97 silver badges124 bronze badges
answered Jul 2, 2012 at 10:42
Sign up to request clarification or add additional context in comments.

Comments

0

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
Glorfindel
22.8k13 gold badges97 silver badges124 bronze badges
answered Jul 2, 2012 at 15:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.