Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
no time scores 1 10 123 2 11 22 3 12 22 4 50 55 5 60 22 6 70 66 . . . . . . nnn 以上是我的txt文件的内容(一千行)。
1st column - number of samples 2nd column - time (from beginning to end ->accumulated) 3rd column - scores 我想创建一个新文件,该文件将是分数的每三个样本的总和除以同一样本的时间差。 eg (123+22+22)/ (12-10) = 167/2 = 83.5 (55+22+66)/(70-50) = 143/20 = 7.15 新的txt文件 83.5 7.15 . . . n 到目前为止,我有此代码: fid=fopen('data.txt') data = textscan(fid,'%*d %d %d') time = (data{1}) score= (data{2}) for sample=1:length(score) ..... // I'm stucked here .. end .... 回答: %# Easier to load with importdata data = importdata('data.txt',' ',1); %# Get the number of rows n = size(data,1); %# Column IDs time = 2;score = 3; %# The interval size (3 in your example) interval = 3; %# Pre-allocate space new_data = zeros(numel(interval:interval:n),1); %# For each new element in the new data index = 1; %# This will ignore elements past the closest (floor) multiple of 3 as requested for i = interval:interval:n %# First and last elements in a batch a = i-interval+1; b = i; %# Compute the new data new_data(index) = sum( data(a:b,score) )/(data(b,time)-data(a,time)); %# Increment index = index+1; end 更多&回答... |
![]() |
![]() |