divide chain code to sub chain code

2 views (last 30 days)
majed majed
majed majed on 9 Feb 2016
Commented: majed majed on 15 Feb 2016
I want to write a function which divide chain code with n elements into m sub chain code where the first sub chain code just contains the first two elements of chain code a with iterations , the second sub chain code contains the next two elements with iterations. For example if we have chain code a a=455454545444544444445444444445444444544444445444444444444444434444343223
I want to divide it into three sub chain code :
a1=4554545454445444444454444444454444445444444454444444444444444
a2=34444343
a2=223
i have written a function for this goals but with low time proficiency . thank you for any answer.

Answers (1)

Geoff Hayes
Geoff Hayes on 10 Feb 2016
majed - I'm not sure if this is more efficient than what you have, but it does produce the answer that you are looking for (assuming the the input chain is a string).
chain='455454545444544444445444444445444444544444445444444444444444434444343223';
setOf2 = chain(1);
startIndex = 1;
subchains = {};
for k=2:length(chain)
if ismember(chain(k),setOf2)
continue;
else
if length(setOf2) == 1
setOf2 = [setOf2 ; chain(k)];
else
subchains = [subchains chain(startIndex:k-1)];
startIndex = k;
setOf2 = chain(k);
end
end
end
subchains = [subchains chain(startIndex:end)];
subchains = [subchains chain(strtIdx:end)];
setOf2 is an array of the two distinct elements that are allowed in the subchain. We iterate over each member of the chain and check to see if it is a member of (contained within) the setOf2. If yes, then we continue to the next element. If not, then we have encountered a third unique element and so have completed the a subchain (which is added to the subchains cell array) and start a new one.
Try the above and see what happens!
  1 Comment
majed majed
majed majed on 15 Feb 2016
thank you for answering .. this code is as same as what i have written . i still wondering if i can improve it. thank you

Sign in to comment.

Categories

Find more on Mathematics 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!