copying every nth line and duplicating it on it's following line

1 view (last 30 days)
I am trying to make test files for the project, and I figured in order to make a bradycardia test file from an example file of a normal ECG. therefore I would need to copy every third line and insert it into the next line.
for example:
a =
[1 2 3 4 5 6 7 8 9 10]
and I want:
b=
[1 2 3 3 4 5 6 6 7 8 9 9 10]
and so on... but since the file is 6000 characters long, obviously i cannot manually copy it. And I need it to be 9000 characters long I've tried looking online on how to do this, and am having no luck. Any suggestions?

Accepted Answer

Cedric
Cedric on 31 Oct 2013
Edited: Cedric on 31 Oct 2013
Here is an example where I display the output so you can see each step:
>> a = [1 2 3 4 5 6 7 8 9 10 11 12]
a =
1 2 3 4 5 6 7 8 9 10 11 12
>> tmp = reshape( a, 3, [] )
tmp =
1 4 7 10
2 5 8 11
3 6 9 12
>> tmp = [tmp; tmp(end,:)]
tmp =
1 4 7 10
2 5 8 11
3 6 9 12
3 6 9 12
>> b = tmp(:).'
b =
1 2 3 3 4 5 6 6 7 8 9 9 10 11 12 12
The only limitation is that the length of a must be a multiple of 3. If it's not the case, you can complete/pad with e.g. NaNs to meet this criterion, and replace the last line which defines b with
>> b = tmp(~isnan(tmp)).'
Let me know if you need to find an automatic way to complete with NaNs.

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!