[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## Re: comparing the magnitude order

**From**: |
Bård Skaflestad |

**Subject**: |
Re: comparing the magnitude order |

**Date**: |
Fri, 9 Sep 2011 12:02:07 +0200 |

On Fri, 2011-09-09 at 11:24 +0200, CdeMills wrote:
>* Hello,*
>* *
>* I would like to compare if two numbers are of the same order of magnitude. I*
>* implement this as*
>* magn =@(x, y) max (x, y)/min (x, y);*
>* *
>* But there are redundancies: if x is > y, then the expression becomes x/y,*
>* else y/x. Is it possible to introduce tests inside function handle ?*
>* magn=@(x, y) if (x > y) x/y else y/x endif*
>* is accepted but doesn't return anything.*
You can't (directly) have branches or assignments or other things that
affect flow of control or memory in an anonymous function constructed as
a single statement. You can, however, achieve the desired effect by
some creative use of function handles.
For example you can, if you're inspired by functional languages, do
something like this
f = { (@(x,y) x./y), (@(x,y) y./x) }
magn = @(x,y) f{2 - (x > y)}(x, y)
Whether or not this is a good idea, readable or maintainable is largely
in the eye of the beholder. Personally, I think I would just use your
original max()/min() expression.
Sincerely,
--
Bård Skaflestad <address@hidden>
SINTEF ICT, Applied Mathematics