我正试图将此代码从Matlab转换为Python:
index = Output_num - 3;
X = Data(1:end - 3, 1:end);
T = Data(end + index:end + index, 1:end);
我测试了很多选项,但没有一个适合我。
我试过了:
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
我不知道你的初始数据是什么样子的,但我从你的回答中假设它是一个2D数组。
对于python中的2D数组:
rows, cols = (10, 10)
Data = [[1]*cols]*rows
要访问这个数组,您需要这样使用行和颜色索引:
print(Data[row_number][col_number])
附加访问和赋值:
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
输出:
Data[0][0] = 1
Data[-1] = [1, 1, 1, 1, 9]