python - 如何将索引从 Matlab 转换为 Python?
问题描述
我正在尝试将此代码从 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
解决方案
我不知道您的初始数据是什么样的,但我从您的回答中假设它是一个二维数组。
对于python中的二维数组:
rows, cols = (10, 10)
Data = [[1]*cols]*rows
要访问此数组,您需要以这种方式同时使用 rows 和 cols 索引:
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]
推荐阅读
- c - 将 hanoi 问题 C 代码转换为 MIPS 程序集
- python - 将 TensorFlow Faster R-CNN 冻结图转换为英特尔 openVINO 框架时出现内部错误
- c# - 使用 RegisterApiControllers 扩展方法时属性注入不起作用
- python - 基于 2d numpy 数组中的索引列表访问行的更有效方法?
- javascript - 如何结合 DOM javascript 和 Blade PHP?
- pyspark - 如何通过在pyspark中添加特定条件的列来丰富数据框?
- amazon-web-services - ECS 任务的任务执行角色 - Cloudformation
- python-3.x - 如何使用 python 连接到 Teradata。我正在使用 teradatasql 包但出现错误
- swift - 如何将这种副作用转化为优雅的 Vapor 操作链?
- django - Django重置密码:无效的令牌