rle.m
来自「matlab实现的游程编码的编码与解码过程」· M 代码 · 共 35 行
M
35 行
function [val,len] = rle(x)
% RLE Run Length Encoding
% [VAL,LEN] = RLE(X) encodes the data in vector X into run values, VAL, and
% run lengths, LEN.
%
% Example:
% >> X = [1 1 1 1 3 3 8 8 8 8 8 8]
% >> [VAL,LEN] = RLE(X)
% Returns:
% X =
% 1 1 1 1 3 3 8 8 8 8 8 8
% VAL =
% 1 3 8
% LEN =
% 4 2 6
%
% See also RLD.
% Steve Hoelzer
% 2003-09-04
% Transpose input vector if necessary
if (size(x,2)==1)
x = x.';
end
% Error checking
if (size(x,1)~=1)
error('RLE can only process vectors, not matrices')
end
% Encode
i = [ find(x(1:end-1) ~= x(2:end)), length(x) ];
len = diff([ 0 i ]); % run lengths
val = x(i); % run values
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?