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

1 次查看(过去 30 天)
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?

采纳的回答

Cedric
Cedric 2013-10-31
编辑:Cedric 2013-10-31
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by