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() ?

0and go ton-1(standard for most programming languages), whereas matlab (and also R) start indexing at1and end atn. Take great care of this difference. As for the solution, the previous comment indicates it well. Takerange(n)if you want it to be more simple. Takerange(1, n+1)if you really need it to go from1tondivide by 0andlog(inf). Do you intend to do?k = np.arange(1,n+1)withk = np.arange(1,n-1). It works.fig = plt.figure()andax = fig.gca()but want to know if the rest 5 lines is the minimal replacement for the 4 line Matlab code.