Index
Index-func
Description
This function is used to return the row index where x occurs in a strictly monotonic dataset, according to condition defined by ctrl.
Syntax
double Index(double x, vector vdata[, int ctrl])
Parameters
x
- is any real number.
vdata
- is the strictly monotonic vector data.
ctrl
- is used to control the index position, which has three options
- 0(default) : Equal or closest
- 1 : Equal or less than
- 2 : Equal or greater than
For data which is monotonically decreasing, the meaning of 1 and 2 are reversed.
Return
Returns the row index where x occurs in vdata according to the ctrl condition. If vdata is not strictly monotonic, has missing values or the data is text, the return value is -2.
Example
for(ii=1;ii<32;ii++) col(1)[ii]=0.5*ii^2;
// Test for value that does not exist in the data
int ind0=Index(170,col(1));
int ind1=Index(170,col(1),1);
int ind2=Index(170,col(1),2);
ind0=; // Should return 18 since (170 - 162) < (180.5 - 170)
ind1=; // Should return 18 since 170 should occur before (less than) row 19
ind2=; // Should return 19 since 170 should occur after (greater than)row 18
// Test for value that does exist in the data
// We can also pass a range variable
range raData = col(1);
int ind0=Index(200,raData);
int ind1=Index(200,raData,1);
int ind2=Index(200,raData,2);
ind0=; // Should return 20
ind1=; // Should return 20
ind2=; // Should return 20
See Also
Xindex
|