有没有办法用python编写这段代码?
我正在将一些Matlab代码翻译成python。在一个脚本中有最后一部分不断给我一个索引错误。我需要帮助来写这个脚本。当试图用python保存数据结构(类似于在Matlab中运行的脚本)时,会出现错误。错误是arr索引 MATLAB代码:有没有办法用python编写这段代码?,python,matlab,Python,Matlab,我正在将一些Matlab代码翻译成python。在一个脚本中有最后一部分不断给我一个索引错误。我需要帮助来写这个脚本。当试图用python保存数据结构(类似于在Matlab中运行的脚本)时,会出现错误。错误是arr索引 MATLAB代码: fid = fopen(fname); scan = textscan(fid, '%f %f %f %f %f'); xx = scan{1,1}; yy = scan{1,2}; th = scan{1,3}; lx = th(1); ly = th(1)
fid = fopen(fname);
scan = textscan(fid, '%f %f %f %f %f');
xx = scan{1,1};
yy = scan{1,2};
th = scan{1,3};
lx = th(1);
ly = th(1);
numsteps = xx(1); % assign number of time steps to first column of data
for tp = 1:numsteps % for loop [] every 50 time steps// range depending on data
[na, cfa, sea] = genstep(xx, yy, th, tp, lx, ly); %input data to genstep() to obtain na, cfa,
sea values. (50 tp)
arr(tp).n = na;
arr(tp).cf = cfa;
arr(tp).se = sea;
end
PYTHON代码:
def arrDisplay(self):
file = np.loadtxt(self.fileName)
xx = file[:, 0]
yy = file[:, 1]
th = file[:, 2]
lx = file[0, 2]
ly = file[0, 2]
numsteps = xx[0]
for tp in range(1, int(numsteps)):
n, cf, se = gensteps(yy, xx, th, tp, lx, ly)
arr = []
arr[tp].append(n)
arr[tp].append(cf)
arr[tp].append(se)
MATLAB数组从1开始,在lenarray结束,而python数组从0开始,在lenarray-1结束。