From ftm
Jump to: navigation, search
 
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  find <expr: expression> <any: arguments to expression...> - leave indices
+
This is a method of the [[FTM_Classes#Class_fmat| fmat class]].
where expression is true (use $x, $i and $self)
+
 
 +
Syntax: <code>find <expr: expression> <any: arguments to expression...> </code>
 +
 
 +
- 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
 
The find method of fmat replaces an fmat with the (unwrapped) indices of the
Line 10: Line 13:
 
latter holds for any expression evaluation.)
 
latter holds for any expression evaluation.)
  
Example: ($myfmat find $myexpr 1 20) with myexpr = '(($x >= $1) && ($i < $2))'
+
Example:  
finds values greater than 1 in the first 20 indices and replaces $myfmat with
+
 
 +
  ($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.
 
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''
 +
 +
[[Category:FAQ]] [[Category:FTM Documentation]] [[Category:fmat Class]] [[Category:expr Class]] [[Category:ftm.mess]]

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:

  1. It is destructive on the input fmat (so copy first if you want to preserve data)
  2. 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.)
  3. 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