Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
回复
 
主题工具 显示模式
旧 2019-12-10, 20:41   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 循环MATLAB问题

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

更多&回答...
poster 当前离线   回复时引用此帖
回复


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 05:15


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.