divide chain code to sub chain code

3 次查看(过去 30 天)
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.

回答(1 个)

Geoff Hayes
Geoff Hayes 2016-2-10
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 个评论
majed majed
majed majed 2016-2-15
thank you for answering .. this code is as same as what i have written . i still wondering if i can improve it. thank you

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Testing Frameworks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by