Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Comparing two files of dissimilar sizes
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Guillaume
il 19 Feb 2018
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 Commenti
Guillaume
il 20 Feb 2018
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');
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!