NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/gliderproc/trunk/MATLAB/util/rlencode.m

Revision 495 (checked in by cbc, 12 years ago)

Initial import of Stark code.

Line 
1 function [len, val] = rlencode(x)
2 %RLENCODE Run-length encode a vector.
3 %   [LEN, VAL] = RLENCODE(X) returns a vector LEN with the length of each
4 %   run and a vector VAL with the corresponding values.  LEN and VAL have
5 %   the same lengths.  X must be a vector.
6 %
7 %   Example: rlencode([ 6 6 4 4 4 5 8 8 7 7 7 7 ]) will return
8 %
9 %      len = [ 2 3 1 2 4 ];     % run lengths
10 %      val = [ 6 4 5 8 7 ];     % values
11 %
12 %   See also RLDECODE.
13 %
14 % Calls: none
15
16 %   Author:      Peter J. Acklam
17 %   Time-stamp:  2000-09-12 11:44:14
18 %   E-mail:      jacklam@math.uio.no
19 %   WWW URL:     http://www.math.uio.no/~jacklam
20
21    % check number of input arguments
22    error(nargchk(1, 1, nargin));
23
24    isiz = size(x);
25    ldim = isiz > 1;
26    if sum(ldim) > 1
27       error('Input must be a vector.');
28    end
29
30    % make sure input is a column vector
31    x = x(:);
32
33    % now perform the run-length encoding
34    i = [ find(x(1:end-1) ~= x(2:end)) ; length(x) ];
35    len = diff([ 0 ; i ]);
36    val = x(i);
37
38    % make sure that the output is a vector in the same dimension as input
39    osiz = isiz;
40    osiz(ldim) = length(len);
41    len = reshape(len, osiz);
42    val = reshape(val, osiz);
Note: See TracBrowser for help on using the browser.