Re: Newbie question about column sums of arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg68545] Re: Newbie question about column sums of arrays
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Wed, 9 Aug 2006 04:19:56 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <eb9pue$t5p$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
George W. Gilchrist wrote:
> I have spent several hours trying to find an answer to what must be
> an incredibly simple problem: how to sum the columns of an array
> containing a random mix of +1 and -1s. For example:
>
> In[7]:=
> AA1=Array[x, {4,4}]/. x:> (-1)^Random[Integer]
>
>
> Out[7]=
> \!\(\*FormBox[
> RowBox[{"(", "\[NoBreak]", GridBox[{
> {\(1[1, 1]\), \(1[1, 2]\), \(\((\(-1\))\)[1, 3]\), \(1[1, 4]
> \)},
> {\(1[2, 1]\), \(1[2, 2]\), \(\((\(-1\))\)[
> 2, 3]\), \(\((\(-1\))\)[2, 4]\)},
> {\(1[3, 1]\), \(\((\(-1\))\)[3, 2]\), \(1[3, 3]\), \(1[3, 4]
> \)},
> {\(\((\(-1\))\)[4, 1]\), \(1[4,
> 2]\), \(1[4, 3]\), \(\((\(-1\))\)[4, 4]\)}
> },
> RowSpacings->1,
> ColumnSpacings->1,
> ColumnAlignments->{Left}], "\[NoBreak]", ")"}],
> TraditionalForm]\)
>
> In[8]:=
> Total[AA1]
>
> Out[8]=
> {(-1)[4,1]+1[1,1]+1[2,1]+1[3,1],
> (-1)[3,2]+1[1,2]+1[2,2]+1[4,2],
> (-1)[1,3]+(-1)[2,3]+1[3,3]+1[4,3],
> (-1)[2,4]+(-1)[4,4]+1[1,4]+1[3,4]}
>
>
> So, Total[] seems to do the right thing, but I cannot get the actual
> sums as real numbers, only this rather verbose representation. I have
> searched the manuals for just about everything I can think of with no
> luck. So, thanks for any help you can give me.
Hi George,
Array does not do what you think. What you want is to use Table [1] in
place of Array to get your array/table/matrix/list_of_list.
AA1 = Table[(-1)^Random[Integer], {4}, {4}]
--> {{-1, 1, -1, 1}, {-1, -1, -1, -1}, {1, 1, -1, -1},
{-1, -1, -1, 1}}
TableForm[AA1]
--> -1 1 -1 1
-1 -1 -1 -1
1 1 -1 -1
-1 -1 -1 1
(* Sum by columns *)
Total[AA1]
--> {-2, 0, -4, 0}
(* Sum by rows *)
Total[Transpose[AA1]]
--> {0, -4, 0, -2}
HTH,
Jean-Marc
[1] http://documents.wolfram.com/mathematica/functions/Table