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

1 visualización (últimos 30 días)
Chace
Chace el 31 de Oct. de 2013
Comentada: Cedric el 31 de Oct. de 2013
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?

Respuesta aceptada

Cedric
Cedric el 31 de Oct. de 2013
Editada: Cedric el 31 de Oct. de 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.

Más respuestas (0)

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by