Convert Integer Array to String Array where repeated integers use "R" for repeated entries

1 回表示 (過去 30 日間)
Alfredezeo
Alfredezeo 2018 年 3 月 23 日
回答済み: Alfredezeo 2018 年 3 月 28 日
The question is a bit vague. An example will make this much more clear. Lets say I have the integer array A
A= [ 1 1 1 1 2 2
2 2 2 2 2 2
3 3 3 2 3 3];
My goal is to have the following array B (it can be character/cell/string) the only requirement is that I can then write B to a text file using fprintf.
B= '1 3R 2 7R 3 2R 2 3 1R';
Where "nR" repeats the preceding entry n times. I am going to be performing this on a large integer array (~ 5.0E+7 elements) so efficiency is crucial.
Thank you for your time.

回答 (3 件)

Alfredezeo
Alfredezeo 2018 年 3 月 28 日
no ideas?

Walter Roberson
Walter Roberson 2018 年 3 月 28 日
編集済み: Walter Roberson 2018 年 3 月 28 日
sprintf('%g %dR ', [values(:), counts(:)-1].' )
or if you are sure that values and counts are row vectors,
sprintf('%g %dR ', [values; counts-1]);

Alfredezeo
Alfredezeo 2018 年 3 月 28 日
Worked like a charm! Thank You Walter

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by