Home > Net >  How to convert indexing from Matlab to Python?
How to convert indexing from Matlab to Python?

Time:10-02

I'm trying to convert this code from Matlab to Python :

index = Output_num - 3;

X = Data(1:end - 3, 1:end);
T = Data(end   index:end   index, 1:end);

I tested many options but any of them work for me.

I tried this way:

index = Output_num -3 #this works good

X = Data[0:-3] # I think this works good ( I compared results with the one from Matlab)

T1 = Data[-1] # with this one I try to access to the last row of the 2d array. The aim was to access on it and then add index on all the rows with the following:

T = T1   index

CodePudding user response:

I don't know what is your initial data looks like, but I assume from your answer that it is a 2D array.

For 2D array in python:

rows, cols = (10, 10)
Data = [[1]*cols]*rows

To access this array, you need to use both rows and cols indices this way:

print(Data[row_number][col_number])

Additional acessing and assigning :

Data[-1][-1] = 9 #assign constant to the last element of the 2D array
print(Data[0][0]) #print the first element of 2D array
print(Data[-1]) #print the last row of 2D array including the assigned number

output:

Data[0][0] = 1
Data[-1] = [1, 1, 1, 1, 9]
  • Related