From: William P. Y. H <wil...@ya...> - 2005-11-11 12:08:19
|
--- Quentin Spencer <qsp...@ie...> wrote: > > If we're aiming for compatibility with the Matlab functions isinteger > and isfloat, we should look at what they do: > >> isinteger(1) > ans = > 0 > > >> isinteger(int8(1)) > ans = > 1 > > So, the outputs of these functions are based not on whether the contents > of the array happen to be integers, which can happen with a double > array, but on whether they are _stored_ as integers. In addition to the > compatibility issue, checking all elements of an array to see if they > are an integer could be much slower with a large array than just > checking the array type. Given this, it would seem that the correct > implementation of isinteger would involve parsing the output of the > typeinfo function. > > -Quentin > Yeah, sorry, I don't have access to a legal copy of Matlab, so... OK, then this should be correct, right? isfloat.m: function retval = isfloat(A) ## -*- texinfo -*- ## @deftypefn {Function File} {} isfloat (@var{A}) ## Return 1 if @var{A} is a floating point array. Otherwise, return 0. ## @end deftypefn ## ## @seealso{isa, isinteger, isnumeric} if (nargin ~= 1) usage("isfloat (A)"); end # Octave doesn't have a single-precision floating point data type switch (class(A)) case "double" retval = 1; otherwise retval = 0; endswitch isinteger.m: function retval = isinteger(A) ## -*- texinfo -*- ## @deftypefn {Function File} {} isinteger (@var{A}) ## Return 1 if @var{A} is an integer array. Otherwise, return 0. ## @end deftypefn ## ## @seealso{isa, isnumeric, isfloat} if (nargin ~= 1) usage("isinteger (A)"); end switch class(A) case "int8" retval = 1; case "int16" retval = 1; case "int32" retval = 1; case "int64" retval = 1; case "uint8" retval = 1; case "uint16" retval = 1; case "uint32" retval = 1; case "uint64" retval = 1; otherwise retval = 0; endswitch issorted.m: function retval = issorted(A,r) ## -*- texinfo -*- ## @deftypefn {Function File} {} issorted (@var{A}, ["rows"]) ## Return 1 if @var{A} is equal to sort(@var{A}), otherwise return 0. ## If "rows" is specified, return 1 if @var{A} is equal to sortrows(@var{A}). ## @end deftypefn ## ## @seealso{sort, sortrows} switch nargin case 1 retval = isequal(A,sort(A)); case 2 if strcmp(r,"rows") retval = isequal(A,sortrows(A)); else usage("issorted (A, [\"rows\"])"); endif otherwise usage("issorted (A, [\"rows\"])"); endswitch isstudent.m: function retval = isstudent ## -*- texinfo -*- ## @deftypefn {Function File} {} isstudent ## Always returns 0, because when you accept Octave's License Agreement ## you always get all the features of Octave and not a stripped-down ## "student" version. ## @end deftypefn ## ## @seealso{ispc, isunix} if (nargin ~= 0) usage("isstudent"); end retval = 0; P.S.: I got excited and wrote those functions too... William Poetra Yoga Hadisoeseno __________________________________ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com |