cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can Bookmark boards, posts or articles that you'd like to access again easily! X

New Match and revised Ceil and Floor

StuartBruff
23-Emerald II

New Match and revised Ceil and Floor

(copied from main Collaboratory)<<br>
Suggestion:

1. Add function Match(z,A,type) that returns vector of indices of elements in A that match z according to the relation specified by type; returns 0 if no matches found. z may a scalar, string or vector, A may be a vector or matrix whose elements are of the same type as z.
type = -1 => greatest values in A less than or equal to z
type = 0 => values in A equal to z
type = +1 => least values in A greater than or equal to z

2. Modify function Ceil(x,y) to return the least value of current Ceil(x,y) if y is a vector or array, and to return x if y (or one of its elements) = 0.

3. Modify function Floor(x,y) to return the maximum value of current Floor(x,y) if y is a vector or array, and to return x if y (or one of its elements) = 0.

Rationale

I often find myself adding tailored variants of match function to deal with the case where there is no match. The current match function generates an error, which requires adding an OnError handler. As match returns a vector when there are matches, I normally make match return the scalar 0 (zero), which is easy to exclude or trap with an if statement if required and clearer to read than an OnError statement.

There are also occasions when I want the nearest match less-than-or-equal or greater-than-or-equal. Examples being assignment of a 'grade' given an exam result or 'shoe size' given a foot length. In a sense they are variants of the lookup functions, but functionally appear to be closer to the spirit of the Ceil and Floor functions. The current (M14) handling of 'y' being a vector can still be handled by using the vectorize operator - M11 Ceil won't handle vector 'y' and M14 Ceil won't handle a vector 'x'. The suggested Floor, Ceil appear to offer greater versatility than the their existing counterparts, and worth the backward compatibility defect.

Discussion Points

M14's Ceil(x,0) returns an error; however, z is the limit of y->0 and it would seem valid to simply return in this case; the attached version of Ceil does this; ditto for Floor.

One area of uncertaintly is what to return when x and y are strings, and the Floor and Ceil functions fail to find a strict match (x < 'least' character in y and x > 'greatest' character in y, respectively). The current Ceil and Floor functions handle the numeric case but character codes don't have the extended range of reals or integers. At the moment, I've entered an empty string, but that seems somehow inadequate.

The attached worksheet also contains:

seq(pattern) - returns a vector of values developed in accordance with pattern; pattern can be a range variable, scalar, character or vector.
- scalar expands to range variable 0..pattern),
- character (expanded to notional range pattern = : uppercase -> "A"..pattern, lowercase -> "a"..pattern, numeric char -> "0"..pattern
- vector of length n, fits first n-1 elements to an (n-1)th polynomial and generates values starting from first element of pattern up to (and possibly including) the nth element of pattern

vec(z) - modified from previous usage; vec converts its inputs into a vector (eg, fully flattens nested arrays or matrices), but now converts a scalar into a single element vector rather than taking it as an implicit range variable (0..z), and transforms a string into a vector of individual characters. seq now handles the generation of a sequence of integers.

mat(z) - converts a string in format [a b c,d e f] into a matrix, where a space is a column separator and a comma is a row separator (same as Matlab). mat also converts an nested vector of vectors into a 2D matrix.

matrix(x,y,f) - accepts vector inputs for x and y, and applies f to each combination of x and y values to generate the output matrix; handles scalars as per built-in variant.

pick(M,list) - modified from previous version; pick will return the elements of M whoses indices are given by list. if M is a vector, then list must be a vector of scalars and if M is a matrix the list must be a vector of nested co-ordinate vectors.

Set(v) - creates a vector of uniques values within v; v may be a vector or matrix

setcount(v) - creates a matrix of uniques values within v and the number of times they appear (frequency table); v may be a vector or matrix

Stuart

http://collab.mathsoft.com/upload/test - floor ceil.mcd
0 REPLIES 0
Top Tags