(Owen Green) |
|||
(One intermediate revision by the same user not shown) | |||
Line 13: | Line 13: | ||
latter holds for any expression evaluation.) | latter holds for any expression evaluation.) | ||
− | Example: | + | Example: |
+ | |||
+ | ($myfmat find $myexpr 1 20) | ||
+ | |||
+ | with myexpr = '(($x >= $1) && ($i < $2))' | ||
+ | |||
finds values greater or equal 1 in the first 20 indices and replaces $myfmat with | finds values greater or equal 1 in the first 20 indices and replaces $myfmat with | ||
their indices. | their indices. | ||
Line 25: | Line 30: | ||
[expr '($x > 0.5)' | myexpr] | [expr '($x > 0.5)' | myexpr] | ||
− | You could do | + | You could do on fmat $foo |
− | (($foo rows 20) ramp 0 1); ($foo find | + | (($foo rows 20) ramp 0 1); ($foo find $myexpr) |
Which would return all those indices where x > 0.5, i.e. the second half of the fmat in this case. | Which would return all those indices where x > 0.5, i.e. the second half of the fmat in this case. |
Latest revision as of 14:25, 3 January 2011
This is a method of the fmat class.
Syntax: find <expr: expression> <any: arguments to expression...>
- leave indices where expression is true (use $x, $i and $self)
The find method of fmat replaces an fmat with the (unwrapped) indices of the elements for which the given expression returned non-zero. The expression can use the arguments to the find method following the expression as $1, $2, etc., and the predefined super-local variables $x (the value of the element being tested), $i (its index), and $self (the reference to the fmat itself). Otherwise, only global definitions are visible within the expression. (The latter holds for any expression evaluation.)
Example:
($myfmat find $myexpr 1 20)
with myexpr = '(($x >= $1) && ($i < $2))'
finds values greater or equal 1 in the first 20 indices and replaces $myfmat with their indices.
More Explanations
find takes an expr and returns the indices of values in the fmat for which the expr is true.
Trivial example: Given
[expr '($x > 0.5)' | myexpr]
You could do on fmat $foo
(($foo rows 20) ramp 0 1); ($foo find $myexpr)
Which would return all those indices where x > 0.5, i.e. the second half of the fmat in this case.
It's a bit like the matlab / octave find function, except that:
- It is destructive on the input fmat (so copy first if you want to preserve data)
- Whereas in matlab / octave there is a simple construct for directly sifting values based on the results of find, newvals = oldvals(find(<expr>)), this is a bit more involved in ftm (because we can't construct arbitrary fvecs based on non-contiguous portions of an fmat, AFAIK.)
- For 2d fmats, find seems to return 'unwrapped' indices rather than coordinates, e.g. for a 20x2 fmat, find will return indices in the range 0<=i<40.
Owen Green