I am defining an array of two’s, with one’s on either end. In MATLAB this can be acheived by
x = [1 2*ones(1,3) 1]
In Python, however, numpy gives something quite different:
import numpy
numpy.array([[1],2*numpy.ones(3),[1]])
What is the most efficient way to perform this MATLAB command in Python?
Alternatively, you could use
hstack:Thanks to DSM for pointing out that pre-allocating the right-sized array from the very beginning, can be much much faster than appending, using
r_orhstackon smaller arrays: