本文介绍了Matlab 中的行程编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 MatLab 很陌生,我有运行长度编码代码,但似乎不起作用,你能帮我吗?
I'm very new with MatLab, I have Run Length Encoding code but it seems to not work, can you help me?
我有这个输入:
ChainCode = 11012321170701000700000700766666666666665555555544443344444333221322222322
我想把它变成 RLE 输出:
and I want make it into RLE output :
(1,2), (0,1), (1,1), (2,1), (3,1), (2,1), (1,2), (7,1), (0,1), (7,1), (0,1),
(1,1), (0,3), (7,1), (0,5), (7,1), (0,2), (7,1), (6,13), (5,8), (4,4), (3,2),
(4,5), (3,3), (2,2), (1,1), (3,1), (2,5), (3,1), (2,2)
这是我的代码:
lengthcode = 1;
N = 1;
for i = 2:length(ChainCode)
if x(i)==x(i-1)
N = N + 1;
valuecode(N) = x(i);
lengthcode(N) = lengthcode(N) + 1;
else
N = 1;
lengthcode = 1;
end
i = i + 1;
end
但这不起作用,我仍然对如何打印这样的输出感到困惑.
But this is not working, and I am still confused about how can I print the output like that.
我希望你能帮助我.谢谢你.
I hope you can help me. Thank you.
推荐答案
这是一个没有循环、cellfun 或 arrayfun 的紧凑解决方案:
Here is a compact solution without loop, cellfun or arrayfun:
chainCode = '11012321170701000700000700766666666666665555555544443344444333221322222322';
numCode = chainCode - '0'; % turn to numerical array
J=find(diff([numCode(1)-1, numCode]));
relMat=[numCode(J); diff([J, numel(numCode)+1])];
这篇关于Matlab 中的行程编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!