Sie sind auf Seite 1von 3

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 %

% % % % % % % % % % % % % % % % % % % % %

len = [ 2 1 1 1 1 2 4 ]; val = [ 6 NaN NaN NaN 5 8 7 ];

% run lengths % values

mttrlencode([ 6 6 NaN NaN NaN 5 8 8 7 7 7 7 ], 1) returns len = [ 2 3 1 2 4 ]; val = [ 6 NaN 5 8 7 ]; See also MTTRLDECODE. 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:05 +0200 pjacklam@online.no http://home.online.no/~pjacklam % run lengths % values

% check number of input arguments nargsin = nargin; error(nargchk(1, 2, nargsin)); sx = size(x); longdim = sx > 1; if sum(longdim) > 1 error('Input must be a vector.'); end if nargin < 2 nanflag = 0; end % make sure input is a column vector x = x(:); % 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); i = x(1:end-1) ~= x(2:end) & ~(isnanx(1:end-1) & isnanx(2:end)); else % this is fast, 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) ; length(x) ]; % end position for each run len = diff([ 0 ; last ]); % length of each run val = x(last); % value of each run % make sure that the output is a vector in the same dimension as input sout = sx; sout(longdim) = length(len); len = reshape(len, sout); val = reshape(val, sout);

if nargout > 2 first = last - len + 1; end

Das könnte Ihnen auch gefallen