1function y = vnorm(A,varargin) 2% VNORM - Return the vector norm along specified dimension of A 3% 4% VNORM(A) returns the 2-norm along the first non-singleton 5% dimension of A 6% VNORM(A,dim) return the 2-norm along the dimension 'dim' 7% VNORM(A,dim,normtype) returns the norm specified by normtype 8% along the dimension 'dim' 9% VNORM(A,[],normtype) returns the norm specified by normtype along 10% the first non-singleton dimension of A 11% 12% normtype may be one of {inf,-inf,positive integer}. 13% For a given vector, v, these norms are defined as 14% inf: max(abs(v)) 15% -inf: min(abs(v)) 16% p (where p is a positive integer): sum(abs(v).^p)^(1/p) 17% 18% Examples: 19% A = [8 1 6; 3 5 7; 4 -9 2]; 20% 21% %Columnwise 2-norm (Euclidean norm) 22% vnorm(A,1) = [9.4340 10.3441 9.4340]; 23% vnorm(A,[],2) % Same as above (since first non-singleton dimensions 24% % is columnwise and default norm is 2-norm. 25% vnorm(A,[],[])% Again, same as above 26% 27% % Row-wise maximum of absolute values 28% vnorm(A,2,inf) = [8 7 9]'; 29% 30% % Columnwise minimum of absolute values 31% vnorm(A,[],-inf) = [3 1 2]; 32% 33% % Error: Use the inf type and not the string 'inf' 34% vnorm(A,[],'inf') % Wrong 35% vnorm(A,[],inf) % Correct 36% 37% 38% Copyright (C) 2009-2017 Dynare Team 39% 40% This file is part of Dynare. 41% 42% Dynare is free software: you can redistribute it and/or modify 43% it under the terms of the GNU General Public License as published by 44% the Free Software Foundation, either version 3 of the License, or 45% (at your option) any later version. 46% 47% Dynare is distributed in the hope that it will be useful, 48% but WITHOUT ANY WARRANTY; without even the implied warranty of 49% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 50% GNU General Public License for more details. 51% 52% You should have received a copy of the GNU General Public License 53% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 54dim = []; 55ntype = []; 56 57if nargin>1 58 dim = varargin{1}; 59 if isempty(dim) 60 idx = find(size(A)~=1); 61 dim = idx(1); 62 elseif dim~=floor(dim) || dim<1 63 error('Dimension must be positive integer'); 64 end 65 if nargin>2 66 ntype = varargin{2}; 67 end 68end 69 70if isempty(dim) 71 idx = find(size(A)~=1); 72 dim = idx(1); 73end 74 75if isempty(ntype) 76 y = sqrt(sum( abs(A).^2 , dim) ); 77elseif ntype==1 78 y = sum( abs(A) , dim ); 79elseif isinf(ntype) 80 if ntype > 0 81 y=max(abs(A), [], dim); 82 else 83 y=min(abs(A), [], dim); 84 end 85elseif ntype~=floor(ntype) || ntype<1 86 error(['Norm type must be one of inf,-inf or a positive ' ... 87 'integer']); 88else 89 y = (sum( abs(A).^ntype , dim) ).^(1/ntype); 90end 91