how to extract specific rows of a .txt file ?
조회 수: 14 (최근 30일)
이전 댓글 표시
I have a problem with a code. I have a file with several rows and columns. Some rows, especially the first six numbers of them, are repeated line by line.
I want a final output that includes only the rows of which the first 6 numbers are repeated.
for example I attach an example of the input and of the output of what I mean
could you please help me?
댓글 수: 0
채택된 답변
Voss
2022년 12월 13일
M = readmatrix('input.txt');
M(:,end) = [];
[~,~,jj] = unique(M,'rows','stable');
M([false; diff(jj) == 0],:) = [];
writematrix(M,'output.txt','delimiter','\t');
% check the output file:
type output.txt
댓글 수: 2
Voss
2022년 12월 15일
M = readmatrix('input.txt');
M(:,end) = [];
[~,~,jj] = unique(M,'rows','stable');
M([false; diff(jj) == 0],:) = [];
M(:,end+1) = diff(find(diff([0; jj; 0])));
writematrix(M,'output.txt','delimiter','\t');
% check the input and output files:
type input.txt
type output.txt
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!