Removing replication in matlab

2 ビュー (過去 30 日間)
Mohamed Jamal
Mohamed Jamal 2020 年 7 月 14 日
コメント済み: Mohamed Jamal 2020 年 7 月 14 日
Hi guys, I have string like this
str= '000000011111100000000000000000000000000000000000111111100000000000000000000011110000000000000000000000000001111111001101'
I want to remove replication of zero and ones in that string this means , the output would be like this '010101010101' without replication of zero or ones ... *be careful that I dont know number of replication of zeros or ones in the str string ...
any help please how can I do that in matlab? thanks alot.

回答 (2 件)

madhan ravi
madhan ravi 2020 年 7 月 14 日
regexprep(str,{'0+','1+'},{'0','1'})
  2 件のコメント
Mohamed Jamal
Mohamed Jamal 2020 年 7 月 14 日
Appreciated!!!!!!
thanks alot
John D'Errico
John D'Errico 2020 年 7 月 14 日
Better than my solution of course.

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


John D'Errico
John D'Errico 2020 年 7 月 14 日
But the string, after you remove the replicates will always be identically the same, just alternating 0's and 1's. If the first element is a '0', then the string will be '010101010...'. If the first element is a 1, then it will be opposite. The only question is the length of the string.
My point is, don't even worry about removing the replicated elements. Just create the final result as it must be.
newstr = str(1);
if newstr == '0'
% Count the number of '10' transitions
newstr = [newstr,repmat('10',[1,numel(strfind(str,'10'))])];
% was the final element a '1'
if str(end) == '1'
newstr = [newstr,'1'];
end
else
% then the first element was a '1'
% So count the number of '01' transitions
newstr = [newstr,repmat('01',[1,numel(strfind(str,'01'))])];
% was the final element a '0'
if str(end) == '0'
newstr = [newstr,'0'];
end
end
For example, with str as in your example, this produces:
newstr =
'010101010101'
  1 件のコメント
Mohamed Jamal
Mohamed Jamal 2020 年 7 月 14 日
Hi all good, I have took this into consideration, already done it ! anyone I appreciate your effort!
could you please help me here because Im really stuck! , it's not good to share you here but any assistance would be appreciated:

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

カテゴリ

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