Delete iterations in a string of numbers

1 回表示 (過去 30 日間)
majed majed
majed majed 2016 年 3 月 19 日
編集済み: Jan 2016 年 3 月 19 日
Ho are you everyone !
I have this string :
S=[222223444451111122666677775555533332222], if I want to delete the iterations in this string to get the string :
ss=[2 3 4 5 1 2 6 7 5 3 2], what is the proper function for this purpose? Thank you a lot for any answer
  1 件のコメント
Jan
Jan 2016 年 3 月 19 日
To be exact: Do you mean:
S = '222223444451111122666677775555533332222'
and
ss = '23451267532'
Which types have S and ss?

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

採用された回答

Stephen23
Stephen23 2016 年 3 月 19 日
編集済み: Stephen23 2016 年 3 月 19 日
Remove repetitions, return a string:
>> str = '222223444451111122666677775555533332222';
>> new = str([true,diff(+str)~=0])
new =
23451267532
It is easy to convert the string into a numeric vector of the digits:
>> vec = new-'0'
vec =
2 3 4 5 1 2 6 7 5 3 2
  1 件のコメント
Jan
Jan 2016 年 3 月 19 日
diff(+str)~=0 is as well really nice as confusing. +1

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

その他の回答 (1 件)

Jan
Jan 2016 年 3 月 19 日
編集済み: Jan 2016 年 3 月 19 日
Or:
S = '222223444451111122666677775555533332222'
T = S(S ~= [S(2:end), 0])
Or if you really want the vector:
U = T - '0'
For a huge data set Fex:RunLength might be faster:
T = RunLength(S);

カテゴリ

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