필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Comparing two files of dissimilar sizes

조회 수: 4 (최근 30일)
Abhinav Srivastava
Abhinav Srivastava 2018년 2월 19일
마감: MATLAB Answer Bot 2021년 8월 20일
I have following two files:
File 1.
0.291851
0.495348
1.162611
-0.495348
-0.203497
0.667262
-0.291851
0.203497
0.870759
-1.761256
-1.469404
-1.265908
-1.162611
-0.870759
-0.667262
File 2.
1 2.000000 4.953483e-01
1 3.000000 2.918515e-01
1 4.000000 1.761256e+00
1 5.000000 1.162611e+00
2 1.000000 -4.953483e-01
2 3.000000 -2.034968e-01
2 4.000000 1.265908e+00
2 5.000000 6.672624e-01
3 1.000000 -2.918515e-01
3 2.000000 2.034968e-01
3 4.000000 1.469404e+00
3 5.000000 8.707592e-01
4 1.000000 -1.761256e+00
4 2.000000 -1.265908e+00
4 3.000000 -1.469404e+00
4 5.000000 -5.986453e-01
5 1.000000 -1.162611e+00
5 2.000000 -6.672624e-01
5 3.000000 -8.707592e-01
5 4.000000 5.986453e-01
File 1 is single column file consisting of 15 rows and File 2 consists of 20 rows and 3 columns. I want to compare each element of file 1 with file 2 and copy whole row matching the element in file 2 in third output file. Any help would be appreciated. Thanks in Advance.

답변 (1개)

Guillaume
Guillaume 2018년 2월 19일
file1 = dlmread('c:\somewhere\file1.txt');
file2 = dlmread('c:\somewhere\file2.txt');
tokeep = ismember(file2(:, 3), file1);
dlmwrite('c:\somewhere\file3.txt', file2(tokeep, :), '\t');
You may need to use ismembertol instead of ismember if the values in both files are not exactly equal.
  댓글 수: 3
Abhinav Srivastava
Abhinav Srivastava 2018년 2월 20일
I have the two input files named as : min_dist.txt and sorted_file.txt. The content of file min_dist.txt is 1.0000 2.0000 0.4953 1.0000 3.0000 0.2919 1.0000 4.0000 1.7613 1.0000 5.0000 1.1626 2.0000 1.0000 -0.4953 2.0000 3.0000 -0.2035 2.0000 4.0000 1.2659 2.0000 5.0000 0.6673 3.0000 1.0000 -0.2919 3.0000 2.0000 0.2035 3.0000 4.0000 1.4694 3.0000 5.0000 0.8708 4.0000 1.0000 -1.7613 4.0000 2.0000 -1.2659 4.0000 3.0000 -1.4694 4.0000 5.0000 -0.5986 5.0000 1.0000 -1.1626 5.0000 2.0000 -0.6673 5.0000 3.0000 -0.8708 5.0000 4.0000 0.5986 and content of file sorted_file.txt is 0.2919 0.4953 1.1626 -0.4953 -0.2035 0.6673 -0.2919 0.2035 0.8708 -1.7613 -1.4694 -1.2659 -1.1626 -0.8708 -0.6673 I am trying to compare elements from file sort_dist.txt with min_dist.txt and then copy lines from min_dist.txt to output. Following is my code: b = load('min_dist.txt') a = load('sorted_file.txt')
for i= 1: length (a)
for j= 1:length(b)
if j >= i
if a(i,1)==b(j,3)
disp(b(j,:))
end
end
end
end
When I am running the code, it gives the following output
1.0000 5.0000 1.1626
4.0000 1.0000 -1.7613
4.0000 3.0000 -1.4694
4.0000 2.0000 -1.2659
5.0000 1.0000 -1.1626
Kindly help me regarding the same.
Thanks in advance
Guillaume
Guillaume 2018년 2월 20일
Using your file names, the code should be:
file1 = dlmread('min_dist.txt');
file2 = dlmread('sorted_file.txt');
tokeep = ismember(file2(:, 3), file1);
dlmwrite('c:\somewhere\file3.txt', file2(tokeep, :), '\t');

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by