我有两个这样的文本文件:
FILE1.TXT
Size Iterations
3 45
5 6
7 50
1 34
5 56
1 4
FILE2.TXT
Size Iterations
3 5
5 6
7 6
1 3
5 6
1 4
我想先做两件事:
1)根据大小对两个文件进行排序。
FILE1.TXT
Size Iterations
1 34
1 4
3 45
5 6
5 56
7 50
FILE2.TXT
Size Iterations
1 3
1 4
3 5
5 6
5 6
7 6
2)一旦文件按大小排序,我想创建一个新文件并存储与每个唯一大小编号对应的所有迭代的平均值
像这样:Size Average
1 3.5
3 5
5 6
7 6
我是MATLAB的新手。请指导我完成上述任务需要遵循哪些功能。
答案 0 :(得分:1)
您可以使用此代码:
%1-Sort everything
s1 = tdfread('File1.txt',' ');%Three space separation
[sorted_s1_size,sorted_s1_idx]=sort(s1.size,'ascend');
sorted_s1_iterations = s1.iterations(sorted_s1_idx);
s2 = tdfread('File2.txt',' ');
[sorted_s2_size,sorted_s2_idx]=sort(s2.size,'ascend');
sorted_s2_iterations = s2.iterations(sorted_s2_idx);
%2-Get the average
sizes = [sorted_s1_size;sorted_s2_size];
iterations = [sorted_s1_iterations,sorted_s2_iterations];
unique_sizes = unique(sizes);
avg_iterations = zeros(1,length(unique_sizes);
for i=1:length(unique_sizes)
w_size = unique_sizes(i);
w_idx=find(sizes==w_size);
avg_iterations(i) = mean(iterations);
end
%4-Write the file
fid = fopen('output.txt','w');
for k=1:length(unique_sizes)
fprintf(fid,'.2f\t%.2f\n',unique_sizes(i),avg_iterations(i));
end
fclose(fid);
您可以优化它,删除两个排序操作(因为unique
也按ID排序)。
答案 1 :(得分:1)
我认为 file1.txt 和 file2.txt 中的第一个原始文件不存在。我的意思是那些包含ASCII字符的行。
clear all
a = load('file1.txt');
b = load('file2.txt');
c = [a;b]; % appends file2 below file1
[aa I] = sort(c(:,1)); % you can find sorted IDs in I
ids = unique(c(I,1));
ave = zeros(1,ids);
for i=1:length(ids)
currentIds = I(c(I,1) == ids(i)); %boolean indexing in sorted IDs
ave(i) = mean(c(currentIds,2));
end
disp([ids,ave'])