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

1 回表示 (過去 30 日間)
Chace
Chace 2013 年 10 月 31 日
コメント済み: Cedric 2013 年 10 月 31 日
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.
  2 件のコメント
Chace
Chace 2013 年 10 月 31 日
you're an angel. thank you!
Cedric
Cedric 2013 年 10 月 31 日
You're welcome!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by