Info
この質問は閉じられています。 編集または回答するには再度開いてください。
Comparing two files of dissimilar sizes
4 ビュー (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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 件のコメント
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!