SHRINK - Removes outer zeros of a matrix. Y = SHRINK(X) removes the outer columns/rows/etc (borders) that contains only zeros on a matrix X. The shrinking goes on untill the first column/row/etc containing a nonzero element is found Examples: SHRINK([0 0 1 0 2 3 0]) = [1 0 2 3] SHRINK([[0 0 0 0 0 0 0] = [[0 0 1 2] 0 0 0 1 2 0 0] [0 0 0 0] 0 0 0 0 0 0 0] [3 0 0 0]] 0 3 0 0 0 0 0]]) Y = SHRINK(X,E) removes columns/rows/etc containing only the element E, intead of zero. [Y,Li,Ls] = SHRINK(...) also returns the limits of the shrinking, Li = inferior limits and Ls = superior limits. See also GROW, ROLL, PAD, PADC.
0001 function [Y, varargout] = shrink(X,E) 0002 0003 % SHRINK - Removes outer zeros of a matrix. 0004 % 0005 % Y = SHRINK(X) removes the outer columns/rows/etc (borders) that 0006 % contains only zeros on a matrix X. The shrinking goes on untill 0007 % the first column/row/etc containing a nonzero element is found 0008 % 0009 % Examples: SHRINK([0 0 1 0 2 3 0]) = [1 0 2 3] 0010 % 0011 % SHRINK([[0 0 0 0 0 0 0] = [[0 0 1 2] 0012 % 0 0 0 1 2 0 0] [0 0 0 0] 0013 % 0 0 0 0 0 0 0] [3 0 0 0]] 0014 % 0 3 0 0 0 0 0]]) 0015 % 0016 % Y = SHRINK(X,E) removes columns/rows/etc containing only the 0017 % element E, intead of zero. 0018 % 0019 % [Y,Li,Ls] = SHRINK(...) also returns the limits of the shrinking, 0020 % Li = inferior limits and Ls = superior limits. 0021 % 0022 % See also GROW, ROLL, PAD, PADC. 0023 % 0024 0025 if nargin==1 0026 E = 0; 0027 end 0028 0029 % If X is row vector transform to col vector (to prevent problems with find) 0030 flagrow = 0; 0031 if ndims(X)==2 & size(X,1)==1 & size(X,2)>=1 0032 X = X'; 0033 flagrow = 1; 0034 end 0035 0036 % Finding columns/rows/etc to be extracted 0037 c = ind2subm( size(X), find(X~=E) ); 0038 0039 % Get minimuns and maximuns founds 0040 li = min(c,[],1)'; 0041 ls = max(c,[],1)'; 0042 0043 % Mounting text: li(1):ls(1),li(2):ls(2)... 0044 N = ndims(X); 0045 for i = 1 : N 0046 ind{i} = li(i):ls(i); 0047 end 0048 0049 % Making Y 0050 Y = X(ind{:}); 0051 0052 % De-transposing is X is row vector 0053 if flagrow 0054 Y = Y'; 0055 end 0056 if nargout>1 0057 varargout{1} = li'; 0058 varargout{2} = ls'; 0059 end