I have few questions regarding string. I have applied Run Length Encoding on a large string. But at the output I am getting some unwanted symbols. why it is so?

1 view (last 30 days)
function y = estring(str)
len = numel(str); %65536
i = 0;
count = zeros(1,len);
y=[];
while( i<len )
j=0;
count(i+1) = 1;
while( true )
j = j + 1;
if( i+j+1 > len )
break;
end
if( str(i+j+1)==str(i+1) )
count(i+1) = count(i+1) + 1;
else
break;
end
end
if false
a=str(i+1);
length(a);
y = [y a];
i = i + 1;
else
a=str(i+1);
b=count(i+1);
y =[y a b];
i = i + b;
if(count==1)
y=[y a b]
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 12 May 2015
I already answered this in a previous discussion. Every second character of your returned string is the char() equivalent of a binary count. Remember, if you have ['P' 9] the result is not 'P9', it is 'P' followed by char(9) which happens to be the tab character. If you want to have 'P9' as the result when the count is 9 then you need to program the code that way and you need to decide exactly what you want to have happen if you get more than 9 in a row of the same thing.
  3 Comments
tina jain
tina jain on 12 May 2015
see Walter sir,I changed the code like this
close all;
clear all;
clc;
X= 'ttPPddgadgtttttttt10 ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt';
lengh=numel(X);
aaAA=estring(X);
zz=numel(aaAA)
y=[];
for i=1:zz
if((~((mod(i,2))==0))&&( (aaAA(i)>='a'&& aaAA(i)<='z') || (aaAA(i)>='A' && aaAA(i)<='Z')))
a=aaAA(i);
y=[y a];
numel(y);
end
i=i+1;
end
%------------------function estring---------------------
function y = estring(str)
len = numel(str);
i = 0;
count = zeros(1,len);
y=[];
while( i<len )
j=0;
count(i+1) = 1;
while( true )
j = j + 1;
if( i+j+1 > len )
break;
end
if( str(i+j+1)==str(i+1) )
count(i+1) = count(i+1) + 1;
else
break;
end
end
if( count(i+1)==1 )
b=1;
a=str(i+1);
length(a);
y = [y a num2str(b)];
i = i + 1;
else
a=str(i+1);
b=count(i+1);
y =[y a num2str(b)];
i = i + b;
end
end
end
%--------OUTPUTS are------------------------------
aaAA =
t2P2d2g1a1d1g1t81101t525
>> y
y =
tPdgadgtt
>> whos
Name Size Bytes Class Attributes
X 1x545 1090 char
a 1x1 2 char
aaAA 1x24 48 char
ans 1x1 8 double
i 1x1 8 double
lengh 1x1 8 double
y 1x9 18 char
zz 1x1 8 double
Is this going right now?what you say?

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!