isivector - Test if parameter is a vector of integers satisfying an optional list of tests. USAGE test = isivector(x,test1,test2,...) x parameter to test test1... optional list of additional tests (see examples below) EXAMPLES % Test if x is a vector of doubles isivector(x) % Test if x is a vector of strictly positive doubles isivector(x,'>0') % Test if x is a vector of doubles included in [2,3] isivector(x,'>=2','<=3') % Special test: test if x is a vector of doubles of length 3 isivector(x,'#3') % Special test: test if x is a vector of strictly ordered doubles isivector(x,'>') NOTE The tests ignore NaNs, e.g. isivector([500 nan]), isivector([1 nan 3],'>0') and isivector([nan -7],'<=0') all return 1. SEE ALSO See also isdmatrix, isdvector, isdscalar, isimatrix, isiscalar, isastring, islscalar, islvector, islmatrix.
0001 %isivector - Test if parameter is a vector of integers satisfying an optional list of tests. 0002 % 0003 % USAGE 0004 % 0005 % test = isivector(x,test1,test2,...) 0006 % 0007 % x parameter to test 0008 % test1... optional list of additional tests (see examples below) 0009 % 0010 % EXAMPLES 0011 % 0012 % % Test if x is a vector of doubles 0013 % isivector(x) 0014 % 0015 % % Test if x is a vector of strictly positive doubles 0016 % isivector(x,'>0') 0017 % 0018 % % Test if x is a vector of doubles included in [2,3] 0019 % isivector(x,'>=2','<=3') 0020 % 0021 % % Special test: test if x is a vector of doubles of length 3 0022 % isivector(x,'#3') 0023 % 0024 % % Special test: test if x is a vector of strictly ordered doubles 0025 % isivector(x,'>') 0026 % 0027 % NOTE 0028 % 0029 % The tests ignore NaNs, e.g. isivector([500 nan]), isivector([1 nan 3],'>0') and 0030 % isivector([nan -7],'<=0') all return 1. 0031 % 0032 % SEE ALSO 0033 % 0034 % See also isdmatrix, isdvector, isdscalar, isimatrix, isiscalar, isastring, 0035 % islscalar, islvector, islmatrix. 0036 % 0037 0038 % Copyright (C) 2010 by Michaƫl Zugaro 0039 % 0040 % This program is free software; you can redistribute it and/or modify 0041 % it under the terms of the GNU General Public License as published by 0042 % the Free Software Foundation; either version 3 of the License, or 0043 % (at your option) any later version. 0044 0045 function test = isivector(x,varargin) 0046 0047 % Check number of parameters 0048 if nargin < 1, 0049 error('Incorrect number of parameters (type ''help <a href="matlab:help isivector">isivector</a>'' for details).'); 0050 end 0051 0052 % Test: double, vector 0053 test = isa(x,'double') & isvector(x); 0054 0055 % Ignore NaNs 0056 x = x(~isnan(x)); 0057 0058 % Test: integers? 0059 test = test & all(round(x)==x); 0060 0061 % Optional tests 0062 for i = 1:length(varargin), 0063 try 0064 if varargin{i}(1) == '#', 0065 if length(x) ~= str2num(varargin{i}(2:end)), test = false; return; end 0066 elseif isastring(varargin{i},'>','>=','<','<='), 0067 dx = diff(x); 0068 if ~eval(['all(0' varargin{i} 'dx);']), test = false; return; end 0069 else 0070 if ~eval(['all(x' varargin{i} ');']), test = false; return; end 0071 end 0072 catch err 0073 error(['Incorrect test ''' varargin{i} ''' (type ''help <a href="matlab:help isivector">isivector</a>'' for details).']); 0074 end 0075 end