Search code examples
pythonmatlabcode-translation

Translating matlab into python


I'm trying to translate a matlab code, and I'm strugeling to translate this part of the code:

[data;SS(ind-1:-1:ind-9)']

In the following context:

SS = 1:288
year = 1:288
data = [];
for ind = 10:length(year)
    data = [data;SS(ind-1:-1:ind-9)'];
end

What I've done at the moment is:

SS = range(1,288);
year = range(1,288);

data = [];

for ind in range(10,length(year)):
    data.append(######)   # code to translate

Solution

  • EDIT: (Output Correction, wrong parameters)

    1:288 is in python list(range(1,289)) or if you use numpy numpy.arange(1,289).

    For index access you have to know, that matlab starts with 1, python with 0, so SS(ind-1:-1:ind-9) becomes SS[ind-2:ind-11:-1]

    SS = list(range(1,289))
    data = []
    for ind in range(9,len(SS)):
        data.append(SS[ind-9:ind][::-1])
    

    or using numpy:

    data = numpy.arange(9,0,-1)[None,:] + numpy.arange(279)[:, None]