Convert array 1:n in matlab to python
Question:
I have question similar to this, and possibly a much simpler one:
We use the
k = 1:n;
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:
>>> arange(1,]10[,1,])
File "<stdin>", line 1
arange(1,]10[,1,])
^
SyntaxError: invalid syntax
>>> arange(1,10[,1,])
File "<stdin>", line 1
arange(1,10[,1,])
^
SyntaxError: invalid syntax
>>> arange([1,]10[,1,])
File "<stdin>", line 1
arange([1,]10[,1,])
^
SyntaxError: invalid syntax
>>> np.arange
<built-in function arange>
>>> arange([1], 10[,1])
File "<stdin>", line 1
arange([1], 10[,1])
^
SyntaxError: invalid syntax
I went to the numpy website and tried to give the syntax there, but again:
>>> import numpy as np
>>> np.arange([1],10[,1],dtype=None)
File "<stdin>", line 1
np.arange([1],10[,1],dtype=None)
^
SyntaxError: invalid syntax
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:
n = 100;
k = 1:n;
z = (n-k)./(n-k-1);
plot(k,log(z))
I ended up writing this in Python:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
# test code to check the shape of a curve
n = 100
k = np.arange(1,n+1)
z = (n - k) / (n - k - 1)
fig = plt.figure()
ax = fig.gca()
plt.plot(k, np.log(z))
plt.show()
Is this the right way of doing it? Or there is a better way of doing the plot()
?
Answers:
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
np.arange(0, n, 1) # will generate 0, 1, ..., n-1
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
np.arange(n) # will generate 0, 1, ..., n-1
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.
For your first question, the simplest ways to do this in numpy are:
import numpy as np
n = 5
k = np.r_[1:n+1]
and:
import numpy as np
n = 5
k = np.r_[:n]+1
The first approach is a tiny bit faster.
For your second question, I see you already found out that you can leave out most of it. There is one additional thing you can do. By default, matplotlib does not update the plot until you do “plt.show()”. However, you can change this by doing something like this:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
plt.interactive(True)
# test code to check the shape of a curve
n = 100
k = np.r_[1:n+1]
z = (n - k) / (n - k - 1)
plt.plot(k, np.log(z))
You only have to do this once when you first start, and you can set it to False at and point later.
You can also configure matplotlib to do this by default by opening your matplotlibrc file and changing:
#interactive : False
to
interactive : True
However, you are better of using IPython, which handles all of this for if you run it using “ipython –pylab”:
from __future__ import division
# test code to check the shape of a curve
n = 100
k = r_[1:n+1]
z = (n - k) / (n - k - 1)
plot(k, log(z))
I have question similar to this, and possibly a much simpler one:
We use the
k = 1:n;
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:
>>> arange(1,]10[,1,])
File "<stdin>", line 1
arange(1,]10[,1,])
^
SyntaxError: invalid syntax
>>> arange(1,10[,1,])
File "<stdin>", line 1
arange(1,10[,1,])
^
SyntaxError: invalid syntax
>>> arange([1,]10[,1,])
File "<stdin>", line 1
arange([1,]10[,1,])
^
SyntaxError: invalid syntax
>>> np.arange
<built-in function arange>
>>> arange([1], 10[,1])
File "<stdin>", line 1
arange([1], 10[,1])
^
SyntaxError: invalid syntax
I went to the numpy website and tried to give the syntax there, but again:
>>> import numpy as np
>>> np.arange([1],10[,1],dtype=None)
File "<stdin>", line 1
np.arange([1],10[,1],dtype=None)
^
SyntaxError: invalid syntax
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:
n = 100;
k = 1:n;
z = (n-k)./(n-k-1);
plot(k,log(z))
I ended up writing this in Python:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
# test code to check the shape of a curve
n = 100
k = np.arange(1,n+1)
z = (n - k) / (n - k - 1)
fig = plt.figure()
ax = fig.gca()
plt.plot(k, np.log(z))
plt.show()
Is this the right way of doing it? Or there is a better way of doing the plot()
?
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
np.arange(0, n, 1) # will generate 0, 1, ..., n-1
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
np.arange(n) # will generate 0, 1, ..., n-1
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.
For your first question, the simplest ways to do this in numpy are:
import numpy as np
n = 5
k = np.r_[1:n+1]
and:
import numpy as np
n = 5
k = np.r_[:n]+1
The first approach is a tiny bit faster.
For your second question, I see you already found out that you can leave out most of it. There is one additional thing you can do. By default, matplotlib does not update the plot until you do “plt.show()”. However, you can change this by doing something like this:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
plt.interactive(True)
# test code to check the shape of a curve
n = 100
k = np.r_[1:n+1]
z = (n - k) / (n - k - 1)
plt.plot(k, np.log(z))
You only have to do this once when you first start, and you can set it to False at and point later.
You can also configure matplotlib to do this by default by opening your matplotlibrc file and changing:
#interactive : False
to
interactive : True
However, you are better of using IPython, which handles all of this for if you run it using “ipython –pylab”:
from __future__ import division
# test code to check the shape of a curve
n = 100
k = r_[1:n+1]
z = (n - k) / (n - k - 1)
plot(k, log(z))