[Date Index]
[Thread Index]
[Author Index]
Re: Using "Sum" (i = 1 ... N) in a function definition
*To*: mathgroup at smc.vnet.net
*Subject*: [mg49828] Re: Using "Sum" (i = 1 ... N) in a function definition
*From*: Paul Abbott <paul at physics.uwa.edu.au>
*Date*: Tue, 3 Aug 2004 01:11:00 -0400 (EDT)
*Organization*: The University of Western Australia
*Sender*: owner-wri-mathgroup at wolfram.com
wilhelm.rainer at gmx.net (Rainer) wrote:
> I was trying to define a function using the Sum command in the
> following way (simplified example):
>
> MyData = {3,2,4,5,6};
> MyFunc[i_] = Sum[MyData[[j]],{j,1,i}];
>
> As you can see, the idea is to use the length of the list (i)
> as a variable in the function definition, such that
>
> MyFunc[1] = 3
> MyFunc[2] = 3 + 2 = 5
> MyFunc[3] = 3 + 2 + 4 = 9
>
> etc. Mathematica does not like the abobe statements, and prints the
> following error message:
>
> "Part specification K$394 is neither an integer nor a list of
> integers."
>
> Does anybody know how to define (correctly) such a function. Should be
> possible ...?
There really is no need to define a function MyFunc. Instead just use
FoldList:
Rest[FoldList[Plus,0,{3,2,4,5,6}]]
Cheers,
Paul
--
Paul Abbott Phone: +61 8 9380 2734
School of Physics, M013 Fax: +61 8 9380 1014
The University of Western Australia (CRICOS Provider No 00126G)
35 Stirling Highway
Crawley WA 6009 mailto:paul at physics.uwa.edu.au
AUSTRALIA http://physics.uwa.edu.au/~paul
Prev by Date:
**Re: The Histogram[] command**
Next by Date:
**Re: envelope of an oscillatory InterpolatingFunction**
Previous by thread:
**Re: Combinations**
Next by thread:
**Re: ListDensityPlot, ColorFunction, and associated confusions**
| |