function x = mttrldecode(len, val) %MTTRLDECODE Run-length decoding of run-length encode data.

% % X = MTTRLDECODE(LEN, VAL) returns a vector XLEN with the length of each % run and a vector VAL with the corresponding values. LEN and VAL must % have the same lengths. % % Example: mttrldecode([ 2 3 1 2 4 ], [ 6 4 5 8 7 ]) will return % % x = [ 6 6 4 4 4 5 8 8 7 7 7 7 ]; % % See also MTTRLENCODE. % % % % % % % % % % % % This program is a part of the MTT (MATLAB Tips & Tricks) Toolbox. Copyright 2002 Peter John Acklam. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. See the file COPYING for details. Author: Time-stamp: E-mail: URL: Peter John Acklam 2003-03-31 18:31:09 +0200 pjacklam@online.no http://home.online.no/~pjacklam

% keep only runs whose length is positive k = len > 0; len = len(k); val = val(k); % now perform the actual run-length decoding i = cumsum(len); % LENGTH(LEN) flops j = zeros(1, i(end)); j(i(1:end-1)+1) = 1; % LENGTH(LEN) flops j(1) = 1; x = val(cumsum(j)); % SUM(LEN) flops

function [len, val, first, last] = mttrlencode(x, nanflag) %MTTRLENCODE Run-length encode a vector. % % [LEN, VAL] = MTTRLENCODE(X) returns a vector LEN with the length of each % run and a vector VAL with the corresponding values. LEN and VAL have % the same lengths. X must be a vector. % % [...] = MTTRLENCODE(X, NANFLAG) will compress sequences of NaN's if % NANFLAG is 1 (or, more precisely, something that MATLAB considers true). % This will slow down MTTRLENCODE a little, though. % % Examples: mttrlencode([ 6 6 4 4 4 5 8 8 7 7 7 7 ]) returns % % len = [ 2 3 1 2 4 ]; % run lengths % val = [ 6 4 5 8 7 ]; % values % % mttrlencode([ 6 6 NaN NaN NaN 5 8 8 7 7 7 7 ]) returns %

nargsin)). This program is a part of the MTT (MATLAB Tips & Tricks) Toolbox. 1) returns len = [ 2 3 1 2 4 ].no http://home.online. Author: Time-stamp: E-mail: URL: Peter John Acklam 2003-03-31 18:31:05 +0200 pjacklam@online. error(nargchk(1. val = [ 6 NaN 5 8 7 ]. i = x(1:end-1) ~= x(2:end) & ~(isnanx(1:end-1) & isnanx(2:end)). sout). % length of each run val = x(last).% % % % % % % % % % % % % % % % % % % % % len = [ 2 1 1 1 1 2 4 ]. but doesn't catch sequences of NaNs i = x(1:end-1) ~= x(2:end). end % now find the run lengths and values last = [ find(i) .'). or (at your option) any later version. length(x) ]. longdim = sx > 1. len = reshape(len. sout(longdim) = length(len). This program is free software. % end position for each run len = diff([ 0 . sx = size(x). you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. % find positions where one element is different from the next if nanflag % catching sequences of NaN's makes this a little tricky isnanx = isnan(x). val = reshape(val.no/~pjacklam % run lengths % values % check number of input arguments nargsin = nargin. else % this is fast. See also MTTRLDECODE. Copyright 2002 Peter John Acklam. sout). either version 2 of the License. end if nargin < 2 nanflag = 0. . val = [ 6 NaN NaN NaN 5 8 7 ]. % value of each run % make sure that the output is a vector in the same dimension as input sout = sx. end % make sure input is a column vector x = x(:). % run lengths % values mttrlencode([ 6 6 NaN NaN NaN 5 8 8 7 7 7 7 ]. if sum(longdim) > 1 error('Input must be a vector. last ]). See the file COPYING for details. 2.

end .if nargout > 2 first = last .len + 1.

Sign up to vote on this title
UsefulNot useful