# Reading a massive file but skipping several lines / rows at a fixed interval

조회 수: 1 (최근 30일)
L'O.G. 2022년 3월 29일
댓글: KSSV 2022년 3월 29일
What is the most efficient way to read / analyze rows 1-1990 of an array, then skip 9 lines, then read rows 2000-3990, skip 9 rows, read rows 4000-5990, and so on to the end of the array? Is there a way to vectorize that?
##### 댓글 수: 5이전 댓글 3개 표시이전 댓글 3개 숨기기
L'O.G. 2022년 3월 29일
Yes, but how? I know how to delete every 4th row, for example, would be A(4:4:end,:) = [] but I don't know how to apply that for my case where I need to skip / delete multiply lines.
Stephen23 2022년 3월 29일
편집: Stephen23 2022년 3월 29일
Use a loop. Inside the loop you can easily tell MATLAB which part of the file to read, e.g.:
• TEXTSCAN lets you specify how many times the format is applied and how many header lines.
• READTABLE et al let you specify the data location, header location, etc.
What is so special about the rows you wan to ignore: are they intermediate headers or just data you don't want? The answer to that question will change how you can approach this task.

댓글을 달려면 로그인하십시오.

### 채택된 답변

KSSV 2022년 3월 29일
편집: KSSV 2022년 3월 29일
id = 1:9 ; % line numbers
V = 1990:2000:9990 ; % give the end wisely
idx = id'+V ; % make indices
idx = idx(:) ; % make indices a column
% Let A be your array
A(idx) = [] ; % remove the lines using the indices
##### 댓글 수: 2없음 표시없음 숨기기
L'O.G. 2022년 3월 29일
Thanks. Very nice. One small typo: the last line should be: A(idx,:) = [];
KSSV 2022년 3월 29일
Yes, if it is an array use A(idx), if it is a matrix, A(idx,:). I have considered it as an array. :)

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Arif Hoq 2022년 3월 29일
try this loop:
A=(1:9990)';　% making an array
idx=2000; % taken value from 2000
count=(size(A,1)+10)/idx;
first_index=A(1:1990); % first index value to make the loop simple
C=cell(count,1);
for i=2:count
C{i}=A(idx*(i-1):(idx*i)-10);
end
mat=[C{:}]

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by