Run Length Encoding in Matlab

后端 未结 4 694
醉话见心
醉话见心 2020-11-27 08:24

I\'m very new with MatLab, I have Run Length Encoding code but it seems to not work, can you help me?

I have this input :

ChainCode  = 110123211707         


        
4条回答
  •  醉酒成梦
    2020-11-27 09:13

    You can avoid the for loop:

    chainCode = '11012321170701000700000700766666666666665555555544443344444333221322222322';
    numCode = chainCode - '0'; % turn to numerical array
    
    % detect edges (changes)
    edges = arrayfun( @(x,y) x ~= y,    ...
                      numCode(1:end-1), ...
                      numCode(2:end));
    % get indexes
    idx = find(edges);
    
    % create tuples
    relMat = cell2mat(arrayfun(         ...
      @(b,e) [ numCode(b) ; e-b+1 ],    ...
      [ 1 (idx + 1) ],                  ...
      [ idx length(numCode) ],          ...
      'UniformOutput', false));
    

提交回复
热议问题