RE: simple questions about matrices
- To: mathgroup at smc.vnet.net
- Subject: [mg26478] RE: [mg26472] simple questions about matrices
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 28 Dec 2000 02:52:18 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Cristian,
mat = Table[Random[Integer, {1, 9}], {i, 3}, {j, 4}]
{{5, 8, 2, 3}, {5, 1, 1, 4}, {8, 5, 5, 8}}
To add the entries in each row, in Version 4 use the shorthand for Apply at
Level 1
Plus @@@ mat
{18, 11, 26}
In Version 3
Plus @@ # & /@ mat
{18, 11, 26}
To add the entries in each column...
columnsum = Plus @@@ Transpose[mat]
{18, 14, 8, 15}
To add a new row with the sum of the columns...
Join[mat, {columnsum}]
{{5, 8, 2, 3}, {5, 1, 1, 4}, {8, 5, 5, 8}, {18, 14, 8, 15}}
To add an extra column...
Transpose[Join[Transpose[mat], {{1, 1, 1}}]]
{{5, 8, 2, 3, 1}, {5, 1, 1, 4, 1}, {8, 5, 5, 8, 1}}
Also, look at the routines in the following package...
Needs["LinearAlgebra`MatrixManipulation`"]
To add a column we append to each row...
AppendRows[mat, Transpose[{{1, 2, 3}}]]
{{5, 8, 2, 3, 1}, {5, 1, 1, 4, 2}, {8, 5, 5, 8, 3}}
To add a row we append to each column...
AppendColumns[mat, {Plus @@@ Transpose[mat]}]
{{5, 8, 2, 3}, {5, 1, 1, 4}, {8, 5, 5, 8}, {18, 14, 8, 15}}
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
> From: Cristian Opazo-Castillo [mailto:cropazo at vassar.edu]
To: mathgroup at smc.vnet.net
>
> Hello all:
>
> I hope that someone is still working and could help me with this...
>
> (a) I have a (m,n) matrix a with integer entries. How can I construct
> a separate list with the totals of adding the entries of either the
> rows or columns?
>
> (b) How can I construct a (m+1,n) matrix where the new row's entries
> are the sums of all numbers on each column?
>
> (c) In general, how can I attach additional columns (o rows) to an
> existing matrix?
>
> Thanks a lot and Happy Holidays,
>
> Cristian
>
>
>