Discussion:
Summing elements within a List or Array
(too old to reply)
John Adeosun
2006-09-14 11:53:42 UTC
Permalink
Hello Everyone,

I generated an intended list of real numbers using Mathematica 5.2, say
"alist", that looks like:

alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)

Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,

aalist = {0, 1.2, 3.5, 6.9,...........}

In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.

I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.

Can anyone help with this problem?

Thanks,

John.
d***@yahoo.com
2006-09-15 10:50:48 UTC
Permalink
***You can work as follows:

In[96]:=
Flatten[Apply[Plus,alist,1],1]
Out[96]=
{0,1.2,3.5,6.9}

***or in compact form

In[95]:=
Plus@@@alist//Flatten
Out[95]=
{0,1.2,3.5,6.9}

***As you can see in the Help Browser and also in (Ted Ersek's Tips and
Tricks)
http://www.verbeia.com/mathematica/tips/HTMLLinks/Tricks_A-K_3.html

***(f@@lst) is normally equivalent to Apply[f, expr] and (f@@@lst)
is normally equivalent to Apply[f, lst, 1] as demonstrated below

In[103]:=
lst={1,2,3};

ln[104]:=
{Apply[f,lst]==f@@lst,Apply[f,lst,1]==f@@@lst}
Out[104]=
{True,True}

***BTW can you tell me how you generate your alist within Mathematica?


Dimitris Anagnostou
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
John Adeosun
2006-09-21 12:16:56 UTC
Permalink
Thanks for the useful suggestion...It works well.
John.
Post by d***@yahoo.com
In[96]:=
Flatten[Apply[Plus,alist,1],1]
Out[96]=
{0,1.2,3.5,6.9}
***or in compact form
In[95]:=
Out[95]=
{0,1.2,3.5,6.9}
***As you can see in the Help Browser and also in (Ted Ersek's Tips and
Tricks)
http://www.verbeia.com/mathematica/tips/HTMLLinks/Tricks_A-K_3.html
is normally equivalent to Apply[f, lst, 1] as demonstrated below
In[103]:=
lst={1,2,3};
ln[104]:=
Out[104]=
{True,True}
***BTW can you tell me how you generate your alist within Mathematica?
Dimitris Anagnostou
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
dh
2006-09-15 10:56:30 UTC
Permalink
Hi John,
this should do it:
alist = {{0}, {{1.2}, 0}, {{2.3}, {1.2}, 0}, {{3.4}, {2.3}, {1.2}, 0}};
Plus @@ Flatten[#] & /@ alist

Daniel
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
Bob Hanlon
2006-09-15 10:59:34 UTC
Permalink
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
{{99.0},{98.9},{97.8},0}};

aalist=Flatten[Total/@alist]

{0,1.2,3.5,6.9,295.7}


Bob Hanlon
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
Sseziwa Mukasa
2006-09-15 11:01:41 UTC
Permalink
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2,
say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to
obtain a
list, say "aalist", having each element equal to the sum of the
elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1
matrix
via summing operation.
I programmed using some summing operations/functions in
Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
aalist=Plus@@Flatten[#]&/@alist

Regards,

Ssezi
Adriano Pascoletti
2006-09-15 11:08:20 UTC
Permalink
Total[Flatten[Most[#]]] & /@ alist

yields

{0, 1.2, 3.5, 6.9}

Adriano Pascoletti
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2,
say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to
obtain a
list, say "aalist", having each element equal to the sum of the
elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1
matrix
via summing operation.
I programmed using some summing operations/functions in
Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
p***@tamu.edu
2006-09-15 11:10:05 UTC
Permalink
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0}};
Map[Total,alist]//Flatten

???
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
Jens-Peer Kuska
2006-09-15 11:14:53 UTC
Permalink
Hi,

I also don't know what you are doing wrong because
aalist = Plus @@ Flatten[#] & /@ alist

will do what you want and

aalist = Total[ Flatten[#]] & /@ alist

will do it and

aalist = Flatten[Total[ #] & /@ alist]

also.

Regards

Jens

"John Adeosun" <***@yahoo.com> schrieb im
Newsbeitrag news:eebfs6$57q$***@smc.vnet.net...
| Hello Everyone,
|
| I generated an intended list of real numbers
using Mathematica 5.2, say
| "alist", that looks like:
|
| alist = {{0}, {{1.2},0},
{{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
|
................{{99.0},{98.9},{97.8},.......,0}}
| (In Matrix form, this "alist" can be shown as a
LowerDiagonal Matrix)
|
| Meanwhile, my present goal is to then program
using Mathematica to obtain a
| list, say "aalist", having each element equal to
the sum of the elements in
| each(corresponding) sublist of the original list
"alist." That is,
|
| aalist = {0, 1.2, 3.5, 6.9,...........}
|
| In essence, it is like reducing, say, 100 by 100
matrix to 100 by 1 matrix
| via summing operation.
|
| I programmed using some summing
operations/functions in Mathematica to no
| avail. I don't know what I 'm doing wrong.
|
| Can anyone help with this problem?
|
| Thanks,
|
| John.
|
|
dkr
2006-09-15 11:18:26 UTC
Permalink
John,

In[12]:=
alist={{0},{{1.2},0},{{2.3},{1.2},0}};

You can exploit the special structure of your list (namely that each
sublist consists of a list together with all the elements of the
previous sublist) by using:

In[13]:=
FoldList[Plus,0,alist[[All,1]]//Flatten]//Rest
Out[13]=
{0,1.2,3.5}

In general, where this special structure may not be present (i.e., the
sublists contain arbitrary elements), you can:
In[14]:=
Plus@@@alist//Flatten
Out[14]=
{0,1.2,3.5}

dkr
b***@ucdavis.edu
2006-09-15 11:22:43 UTC
Permalink
John,

Try the following:

In[10]:=
Map[Total[Flatten[#]]&,alist]

Out[10]=
{0,1.2,3.5,6.9}

Cheers,

Brian
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
gardyloo
2006-09-15 11:25:59 UTC
Permalink
Plus @@ (# // Flatten) & /@ alist



(flatten each row/column of the list, then apply Plus to each of the
results)
I think that'll do what you want.

Cheers,
C.O.
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
--
==========================================================
Curtis Osterhoudt
***@mail.remove_this.wsu.and_this.edu
PGP Key ID: 0x088E6D7A
Please avoid sending me Word or PowerPoint attachments
See http://www.gnu.org/philosophy/no-word-attachments.html
==========================================================
Jean-Marc Gulliet
2006-09-15 11:32:26 UTC
Permalink
Post by John Adeosun
Hello Everyone,
I generated an intended list of real numbers using Mathematica 5.2, say
alist = {{0}, {{1.2},0}, {{2.3},{1.2},0},{{3.4},{2.3},{1.2},0},
................{{99.0},{98.9},{97.8},.......,0}}
(In Matrix form, this "alist" can be shown as a LowerDiagonal Matrix)
Meanwhile, my present goal is to then program using Mathematica to obtain a
list, say "aalist", having each element equal to the sum of the elements in
each(corresponding) sublist of the original list "alist." That is,
aalist = {0, 1.2, 3.5, 6.9,...........}
In essence, it is like reducing, say, 100 by 100 matrix to 100 by 1 matrix
via summing operation.
I programmed using some summing operations/functions in Mathematica to no
avail. I don't know what I 'm doing wrong.
Can anyone help with this problem?
Thanks,
John.
alist = {{0}, {{1.2}, 0}, {{2.3}, {1.2}, 0}, {{3.4}, {2.3}, {1.2}, 0},
{{99.}, {98.9}, {97.8}, 0}};

aalist = Flatten[Apply[Plus, alist, {1}]]

--> {0, 1.2, 3.5, 6.9, 295.7}

Jean-Marc
Loading...