Skip to content
Advertisement

Convert array 1:n in matlab to python

I have question similar to this, and possibly a much simpler one:

We use the

JavaScript

a lot in Matlab. But in Python I simply struggle to get this simple thing work. I tried using arange, but never really realized what the error is:

JavaScript

I went to the numpy website and tried to give the syntax there, but again:

JavaScript

I wouldn’t have written this post just to clarify something of this sort, but my point is why is this simplest Matlab command so very complicated in Python? I even used this tool to convert .m codes to .py codes, with little effect.

EDIT after the post from @mskimm: Thanks! One related question. To write something very similar to the following in Matlab:

JavaScript

I ended up writing this in Python:

JavaScript

Is this the right way of doing it? Or there is a better way of doing the plot() ?

Advertisement

Answer

Welcome to Numpy!

1:n is the same as arange(1, n+1, 1) where the first 1 is start, n+1 is stop, and the last 1 is step. Note that Numpy index is started at 0 whereas Matlab is 1. So I think

JavaScript

is more appropriate to array indexing.

[] means optional value. If you don’t give an explicit value, start is 0 and step is 1. You can use

JavaScript

This page helps Matlab users.

UPDATE

Comment for Matlab Users who start learning Numpy and is familiar with Matlab IDE.

I recommend to use Spyder or IPython Notebook.

Spyder IPython Notebook

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement