read certain range in csv file and save to new file

2 views (last 30 days)
Hi guys,
I have a one column data contain 100 000 rows.
i need to extract a data for range 0-0 and save it into new file.
example of my data:
0
1
2
3
4
5
0
6
7
4
3
5
6
0
6
43
5
7
5
0
7
4
60
0
Then, for a new file it should contains 0 1 2 3 4 5 0 in column , the second file is 0 6 7 4 3 5 6 0 and so on.

Accepted Answer

Star Strider
Star Strider on 16 Mar 2019
A loop is the only (and likely the simplest) option, since you need the zero values on both ends of each sub-vector:
V = [0
1
2
3
4
5
0
6
7
4
3
5
6
0
6
43
5
7
5
0
7
4
60
0];
idx = find(V == 0);
for k1 = 1:numel(idx)-1
Out{k1} = V(idx(k1):idx(k1+1));
end
  4 Comments

Sign in to comment.

More Answers (1)

dpb
dpb on 16 Mar 2019
Same basic solution but one can write the loop without an explicit loop -- of course, it could easily be found to be less efficient than the deadahead solution besides the complexity, but just as exercise:
idx=find(V == 0);
idx=reshape([idx(1);kron(idx(2:end-1),ones(2,1));idx(end)],2,[]);
out=arrayfun(@(i1,i2) V(i1:i2),idx(1,:),idx(2,:),'uni',0);

Community Treasure Hunt

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

Start Hunting!