Hello, If I have a string, S = 001000110111 Given:
a = 0
b = 10
c = 110
d = 111
I need to convert the string using that key.
So converted_S = aabaacd
How can I do this? Thanks.

 채택된 답변

Grzegorz Knor
Grzegorz Knor 2012년 3월 20일

0 개 추천

S = '001000110111';
a = '0';
b = '10';
c = '110';
d = '111';
S = strrep(S,d,'d');
S = strrep(S,c,'c');
S = strrep(S,b,'b');
S = strrep(S,a,'a')

댓글 수: 5

Brandon
Brandon 2012년 3월 20일
Nice solution, but this doesn't quite work without error. My actual string is 256,000 characters, with this I get a string with length 258,446. Seems like strrep doesn't go in order.
Brandon
Brandon 2012년 3월 20일
Example:
X = 'aabdcaaabcdcdba';
W = '001011111000010110111110111100';
This code produces:
W = 'aabdddaaaabcdddaddaa'
Brandon
Brandon 2012년 3월 20일
Figured it out! Use regexprep instead of strrep. Thanks for leading me in the right direction, much better than the for loop I was trying to use.
Grzegorz Knor
Grzegorz Knor 2012년 3월 20일
regexp works correctly in this case :) strrep first looks for patterns, and then replace it.
Abirami
Abirami 2014년 8월 14일
hello, i need to do the same thing for a 256x256 matrix...pls help...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

질문:

2012년 3월 20일

댓글:

2014년 8월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by