Datafilter - match()

.. match(vector1, vector2) - returns an index of values; for each value
   in vector1 it will return the index into vector2 if there. if the
   element in vector1 is not found in vector2 no entry will be added to
   the returned index.

.. e.g:

   a <- c(4,6,9);
   b <- c(1,2,3,4,5,6,7,8,9);
   d <- match(a,b);
   # d now contains 3,5,8 since indexes start from 0
This commit is contained in:
Mark Liversedge
2020-05-08 12:58:30 +01:00
parent c1edbcb00a
commit 4029c3a9aa

View File

@@ -242,6 +242,9 @@ static struct {
// the beta (coefficients) for each x series 1-n, the covariance matrix
// is discarded for now. we could look at that later
{ "match", 2 }, // match(vector1, vector2) - returns a vector of indexes. For every element in vector1
// that is in vector2, the index of the first occurrence is returned.
// add new ones above this line
{ "", -1 }
};
@@ -423,6 +426,10 @@ DataFilter::builtins()
returning << "mlr(yvector, xvector .. xvector)";
} else if (i == 82) {
returning << "match(vector1, vector2)";
} else {
QString function;
@@ -3556,6 +3563,31 @@ Result Leaf::eval(DataFilterRuntime *df, Leaf *leaf, float x, long it, RideItem
return returning;
}
// match
if (leaf->function == "match") {
// for every value in vector 1 return the index for it
// in vector 2, if it is not there then it will not be
// included in the returned index
Result returning(0);
Result v1 = eval(df,leaf->fparms[0],x, it, m, p, c, s, d); // lhs might also be a symbol
Result v2 = eval(df,leaf->fparms[1],x, it, m, p, c, s, d); // lhs might also be a symbol
// lets search
for(int it=0; it<v1.vector.count(); it++) {
double find = v1.vector[it];
for(int it2=0; it2<v2.vector.count(); it2++) {
if (v2.vector[it2] == find) {
returning.number += it2;
returning.vector << it2;
}
}
}
return returning;
}
// annotate
if (leaf->function == "annotate") {