Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Newbie question about column sums of arrays

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68569] Re: Newbie question about column sums of arrays
  • From: Torsten Coym <torsten.coym at eas.iis.fraunhofer.de>
  • Date: Wed, 9 Aug 2006 23:57:11 -0400 (EDT)
  • References: <eb9pue$t5p$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

George,

The array you provided does not contain 1 and -1, but 1[1,1], -1[1,3] 
and so forth, which are completely different expressions. To get a 4x4 
matrix with random 1 and -1 you could instead write:

AA1 = Array[(-1)^Random[Integer] & , {4, 4}]

{{1, 1, -1, 1}, {1, 1, 1, 1}, {1, -1, 1, -1},
   {1, -1, 1, -1}}

Or:

AA1 = Array[x, {4, 4}] /. _x :> (-1)^Random[Integer]

which replaces everything that /has a head x/ with Random 1 or -1, while 
your replacement rule replaces everything that /is the object x/.


Then Total[] does exactly what you want:

Total[AA1]

Out[892]=
{4, 0, 2, 0}

Torsten

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.
> 
> 
> Cheers, George
> 
> ..................................................................
> George W. Gilchrist                        Email #1: gwgilc at wm.edu
> Department of Biology, Box 8795          Email #2: kitesci at cox.net
> College of William & Mary                    Phone: (757) 221-7751
> Williamsburg, VA 23187-8795                    Fax: (757) 221-6483
> http://gwgilc.people.wm.edu/
> 
> 


  • Prev by Date: Simple Plot3D/Function Syntax issue
  • Next by Date: inflated nb-files
  • Previous by thread: Re: Newbie question about column sums of arrays
  • Next by thread: Multiplying Elements of arrays: Inner product