下面的代码给出了error = Subscript indices must either be real positive integers or logicals.
因为第一个使用FIND函数的索引是i = 1,所以i-1
给出了负值。如何在不使用LOOP
函数的情况下i == 1时忽略
fid=fopen('data.txt');
A=textscan(fid,'%f%f%f%f');
fclose(fid);
in = cell2mat(A);
i = find(in(1:end,2)>20 & in(1:end,2) <50);
out=in;
s(i)=sqrt((out(i,3)-out(i-1,3))*(out(i,3)-out(i-1,3))+(out(i,4)-out(i-1,4))*(out(i,4)-out(i-1,4)));
fid = fopen('newData.txt','wt');
format short g;
fprintf(fid,'%g\t%g\t%g\t%g\n',out',s'); %'# Write the data to the file
fclose(fid);
答案 0 :(得分:2)
i = find(in(1:end,2)>20 & in(1:end,2) <50);
i = i(find( i > 1));
顺便说一句,小心使用i
作为常规变量,因为你要覆盖默认值:
i = sqrt(-1)