Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
Chace
Chace am 31 Okt. 2013
Kommentiert: Cedric am 31 Okt. 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?

Akzeptierte Antwort

Cedric
Cedric am 31 Okt. 2013
Bearbeitet: Cedric am 31 Okt. 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.

Weitere Antworten (0)

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by