Re: behavior when used inside of Module.
- To: mathgroup at smc.vnet.net
- Subject: [mg117091] Re: behavior when used inside of Module.
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Wed, 9 Mar 2011 07:01:40 -0500 (EST)
On 3/8/11 at 5:36 AM, dwaynedcole at gmail.com (Dwayne) wrote:
>This works:
>Position[A, Min[A]]]
>where, A = {5.3, 5.4, 5.2, 5.9}. the Position function returns ( 3).
>But this doesn't
>Module[{ A, B, RETURN},
>A = Table[ F, {i,{.1, .2, .3, .4}]; (* The function F
>returns {5.3, 5.4, 5.2, 5.9} *)
>B = Position[A, Min[A]]; RETURN = B]
>The above module returns, { }.
There is a missing } in the code above. Presumably you meant
Table[ F, {i,{.1, .2, .3, .4}}]
instead of
Table[ F, {i,{.1, .2, .3, .4}]
If the point of this code is to return the position of the
smallest element after using F to operate on the specific array
supplied, it is overly complex. The local variables B and RETURN
are unnecessary. That is
Module[{ A},
A = Table[ F, {i,{.1, .2, .3, .4}];
Position[A, Min[A]]]
Nothing at all is gained by having the additional local
variables. This code can be made even more compact and
efficient. The function Ordering with the second argument is a
more efficient way of obtaining the position of the smallest
element in a list. Specifically,
In[1]:= a = {5.3, 5.4, 5.2, 5.9};
Ordering[a, 1]
Out[2]= {3}
So, your code can be made into a one-liner
Ordering[Table[F, {i, .1, .4, .1}], 1]