Short version: the most idiomatic and fastest way to do what you want in python is this (assuming x_data
is a numpy array):
x_data_train = np.vstack([x_data[0:20,:],
x_data[46:65,:],
x_data[91:110,:],
x_data[136:155,:],
x_data[181:200,:],
x_data[226:245,:],
x_data[271:290,:],
x_data[316:335,:]])
This can be shortened (but made very slightly slower) by doing:
xdata[np.r_[0:20,46:65,91:110,136:155,181:200,226:245,271:290,316:335], :]
For your case where you have a lot of indices I think it helps readability, but in cases where there are fewer indices I would use the first approach.
Long version:
There are several different issues at play here.
First, in python, []
makes a list, not an array like in MATLAB. Lists are more like 1D cell arrays. They can hold any data type, including other lists, but they cannot have multiple dimensions. The equivalent of MATLAB matrices in Python are numpy arrays, which are created using np.array
.
Second, [x, y]
in Python always creates a list where the first element is x
and the second element is y
. In MATLAB [x, y]
can do one of several completely different things depending on what x
and y
are. In your case, you want to concatenate. In Python, you need to explicitly concatenate. For two lists, there are several ways to do that. The simplest is using x += y
, which modifies x
in-place by putting the contents of y
at the end. You can combine multiple lists by doing something like x += y + z + w
. If you want to keep x
, unchanged, you can assign to a new variable using something like z = x + y
. Finally, you can use x.extend(y)
, which is roughly equivalent to x += y
but works with some data types besides lists.
For numpy arrays, you need to use a slightly different approach. While Python lists can be modified in-place, strictly speaking neither MATLAB matrices nor numpy arrays can be. MATLAB pretends to allow this, but it is really creating a new matrix behind-the-scenes (which is why you get a warning if you try to resize a matrix in a loop). Numpy requires you to be more explicit about creating a new array. The simplest approach is to use np.hstack
, which concatenates two arrays horizontally (or np.vstack
or np.dstack
for vertical and depth concatenation, respectively). So you could do z = np.hstack([v, w, x, y])
. There is an append
method and function in numpy, but it almost never works in practice so don't use it (it requires careful memory management that is more trouble than it is worth).
Third, what append
does is to create one new element in the target list, and put whatever variable append
is called with in that element. So if you do x.append([1,2,3])
, it adds one new element to the end of list x
containing the list [1,2,3]
. It would be more like x = [x, {{1,2,3}}}
, where x
is a cell array.
Fourth, Python makes heavy use of "methods", which are basically functions attached to data (it is a bit more complicated than that in practice, but those complexities aren't really relevant here). Recent versions of MATLAB has added them as well, but they aren't really integrated into MATLAB data types like they are in Python. So where in MATLAB you would usually use sum(x)
, for numpy arrays you would use x.sum()
. In this case, assuming you were doing appending (which you aren't) you wouldn't use the np.append(x, y)
, you would use x.append(y)
.
Finally, in MATLAB x:y
creates a matrix of values from x
to y
. In Python, however, it creates a "slice", which doesn't actually contain all the values and so can be processed much more quickly by lists and numpy arrays. However, you can't really work with multiple slices like you do in your example (nor does it make sense to because slices in numpy don't make copies like they do in MATLAB, while using multiple indexes does make a copy). You can get something close to what you have in MATLAB using np.r_
, which creates a numpy array based on indexes and slices. So to reproduce your example in numpy, where xdata
is a numpy array, you can do xdata[np.r_[1:20,46:65,91:110,136:155,181:200,226:245,271:290,316:335], :]