GROW Expands a matrix. Y = GROW(X,[M1 M2 ... Mn]) creates a matrix 'Y' by the expansion of the matrix 'x' of Mi elements on directions i e -i. The size of the new matrix 'Y' will be: size(Y) = size(x) + 2*[M] Example: X = [ 1 2 3; 4 5 6; 7 8 9] Y = GROW(X,[1 0]) Y = [1 2 3; 1 2 3; 4 5 6; 7 8 9; 7 8 9] Y = GROW(X,[1 1]) Y = [1 1 2 3 3; 1 1 2 3 3; 4 4 5 6 6; 7 7 8 9 9; 7 7 8 9 9] Y = GROW(X) uses as standard Mi = 1. If DIM(X)>1, GROW(X,N) = GROW(X,N*ones(DIM(x))) Y = GROW(X,[M], 'a') performs assimetric expansion. If Mi is even assimetric expansion is the same as simmetric expansion, execpt that the expansion in the assimetric mode will be half of the one in the simmetric mode. If Mi is odd, the matrix will be expanded ceil(Mi/2) elements in the -i direction and floor(Mi/2) in the i. The size of the expanded matrix will be: size(y) = size(X) + [M] [M] can contain negative integers. In this case, the outer elements of X will be discarded. See also ROLL, PAD, PADC, SHRINK.
0001 function Y = grow(varargin) 0002 % 0003 % GROW Expands a matrix. 0004 % 0005 % Y = GROW(X,[M1 M2 ... Mn]) creates a matrix 'Y' by the expansion 0006 % of the matrix 'x' of Mi elements on directions i e -i. The size 0007 % of the new matrix 'Y' will be: 0008 % size(Y) = size(x) + 2*[M] 0009 % 0010 % Example: X = [ 1 2 3; 4 5 6; 7 8 9] 0011 % Y = GROW(X,[1 0]) 0012 % Y = [1 2 3; 1 2 3; 4 5 6; 7 8 9; 7 8 9] 0013 % 0014 % Y = GROW(X,[1 1]) 0015 % Y = [1 1 2 3 3; 1 1 2 3 3; 4 4 5 6 6; 7 7 8 9 9; 7 7 8 9 9] 0016 % 0017 % Y = GROW(X) uses as standard Mi = 1. 0018 % 0019 % If DIM(X)>1, GROW(X,N) = GROW(X,N*ones(DIM(x))) 0020 % 0021 % Y = GROW(X,[M], 'a') performs assimetric expansion. If Mi is even 0022 % assimetric expansion is the same as simmetric expansion, execpt that 0023 % the expansion in the assimetric mode will be half of the one in the 0024 % simmetric mode. If Mi is odd, the matrix will be expanded ceil(Mi/2) 0025 % elements in the -i direction and floor(Mi/2) in the i. The size of 0026 % the expanded matrix will be: 0027 % size(y) = size(X) + [M] 0028 % 0029 % [M] can contain negative integers. In this case, the outer elements 0030 % of X will be discarded. 0031 % 0032 % See also ROLL, PAD, PADC, SHRINK. 0033 % 0034 0035 0036 [X, M, assimetric] = parse_inputs(varargin{:}); 0037 0038 ind = cell(length(M),1); 0039 0040 if assimetric == 0 0041 for i = 1 : length(M) 0042 if M(i) >= 0 0043 ind{i} = [repmat(1,[1 M(i)]) 1:size(X,i) repmat(size(X,i),[1 M(i)]) ]; 0044 else 0045 ind{i} = 1+(-M(i)) : size(X,i)-(-M(i)); % using -M(i) to change sign 0046 end 0047 end 0048 else % if not assimetric 0049 for i = 1 : length(M) 0050 if M(i) >= 0 0051 ind{i} = [repmat(1,[1 ceil(M(i)/2)]) 1:size(X,i) repmat(size(X,i),[1 floor(M(i)/2)]) ]; 0052 else 0053 ind{i} = 1+ceil(-M(i)/2) : size(X,i) - floor(-M(i)/2); 0054 end 0055 end 0056 end 0057 0058 Y = X(ind{:}); 0059 0060 0061 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0062 function [X,M,assimetric] = parse_inputs(varargin) 0063 assimetric = 0; 0064 0065 switch nargin 0066 case 0 0067 error('Too few inputs!') 0068 return 0069 0070 case 1 0071 X = varargin{1}; 0072 M = ones(1,ndims(X)); 0073 0074 case 2 0075 X = varargin{1}; 0076 M = varargin{2}; 0077 if strcmp(M,'a') 0078 M = ones(1,ndims(X)); 0079 assimetric = 1; 0080 end 0081 0082 case 3 0083 X = varargin{1}; 0084 M = varargin{2}; 0085 if ~strcmp('a', lower(varargin{3})) 0086 error('Unknown parameter.'); 0087 end 0088 assimetric = 1; 0089 0090 case 4 0091 error('Too many inputs!') 0092 return 0093 end 0094 0095 if length(M)==1 0096 if sum(size(X)>1)>1 0097 M = M*ones(1,ndims(X)); 0098 elseif ndims(X)==2 & size(X,1)==1 & size(X,2)>1 0099 M = [0 M]; 0100 elseif ndims(X)==2 & size(X,1)>1 & size(X,2)==1 0101 M = [M 0]; 0102 end 0103 end 0104 0105 if length(M)~=ndims(X) 0106 error('Invalid dimensions') 0107 end 0108