MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: List Replace Problems

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59853] Re: [mg59831] List Replace Problems
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Wed, 24 Aug 2005 06:30:21 -0400 (EDT)
  • References: <200508230851.EAA03004@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On Aug 23, 2005, at 4:51 AM, wesh wrote:

>
> I want to perform some element by element operations on a class of
> lists.

In Mathematica typically you want to use Map or in your case  
MapIndexed to perform this operation, not a Do loop.

> For a given list The operation in a much simplified form can be
> characterized by
>
> a = {1, 3, 5, 7, 9};
> Do[a[[i]] /= i, {i, 1, 5}];
> a

Equivalent results can be obtained with

a={1,3,5,7,9};
a=MapIndexed[#/#2[[1]]&,a]

> Now, however, if I try to define a function to do this for a general
> list, namely,
>
> dlst[u_List] =
>    Do[u[[i]] /= i,  {i, 1, 5}];

This doesn't work because you used Set instead of SetDelayed to  
define your function and the right hand side has already been  
evaluated.  Furthermore in Mathematica functions typically do not  
modify their arguments so you should not try to assign to u (which as  
your error indicates will be a list not a symbol in your expression).

Using MapIndexed you can define the function

dlstFunctional[u_List]:=MapIndexed[#/#2[[1]]&,u]

If you want to destructively update the values in your list simply  
assign the result of the function to the original symbol eg.

a={1,3,5,7,9};
a=dlstFunctional[a]

If you really want to use an explicit loop use Table not Do:

dlstExplicitLoop[u_List]:=Table[u[[i]]/i,{i,Length[u]}]

Regards,

Ssezi


  • Prev by Date: Re: a question about intersection curve.
  • Next by Date: Re: Distribution and Integral
  • Previous by thread: Re: List Replace Problems
  • Next by thread: Re: List Replace Problems