Re: simple vactor magitude question
- To: mathgroup at smc.vnet.net
- Subject: [mg43771] Re: simple vactor magitude question
- From: "John Jowett" <John.Jowett at cern.ch>
- Date: Sat, 4 Oct 2003 02:04:51 -0400 (EDT)
- Organization: CERN
- References: <blj5e0$1dv$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Assuming p is a simple list, something like
p={1,2,5}
then the list index should start at zero (unlike the C programming
language).
In Mathematica, there is usually no need to loop over an index with the
advantage that this kind of error can be avoided. Other ways to get your
result might be
p.Range[Length[p]
or
Plus@@MapIndexed[Times[#1,First[#2]]&,p]
The latter method readily generalises to more complicated examples.
John Jowett
"Nathan Moore" <nmoore at physics.umn.edu> wrote in message
news:blj5e0$1dv$1 at smc.vnet.net...
> Suppose I generate a 1-d vecctor of "N" numbers, p[i], and now I want to
> find the sum of (the vector element * index). I think the syntax should
look
> something like,
>
> Sum[p[i]*i,{i,0,N}]
>
> but this doesn't seem to be the correct syntax - any help would be
> appreciated.
>
> Nathan Moore,
> University of Minnesota Physics
>