MathGroup Archive 2005

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

Search the Archive

Re: Add terms surrounded by zero together in matrix

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59192] Re: Add terms surrounded by zero together in matrix
  • From: Peter Pein <petsie at dordos.net>
  • Date: Mon, 1 Aug 2005 01:05:06 -0400 (EDT)
  • References: <dcccur$3j7$1@smc.vnet.net><dcf31j$led$1@smc.vnet.net> <dchoka$86g$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

mchangun at gmail.com schrieb:
> Hi Oli,
> 
> That should be counted as two 1's.
> 
Andrzeij's code is easily adapted to this:

In[1]:=
SumsOfTermsSurroundedByZero[AA_]:=
  Block[{d,MakeNames,A},
    A=Prepend[Flatten[{0,#,0}]&/@AA,Table[0,{2+Length[AA[[1]]]}]];
    MakeNames[i_,j_]:=A[[i,j]]*=
        If[(d=Variables[{A[[i-1,j]],A[[i,j-1]]}])==={},
          Unique[z],
          If[Length[d]>1,
            Set[#,First[d]]&/@Rest[d]];
          First[d]
          ];
    MakeNames@@@Position[A,_?Positive];
    Coefficient[Plus@@Flatten[A],#]&/@Variables[A]
    ]

In[2]:=
SumsOfTermsSurroundedByZero[{{1,0,0,1,0,0,1},{0,1,0,1,0,
  1,0},{0,0,1,0,1,0,0},{0,0,0,1,0,0,0}}]

Out[2]=
{1,2,1,1,1,1,1,1}

-- 
Peter Pein
Berlin
http://people.freenet.de/Peter_Berlin/


  • Prev by Date: Re: Mathematica's CPU utilization
  • Next by Date: Re: BlankSequence
  • Previous by thread: Re: Mathematica's CPU utilization
  • Next by thread: Re: BlankSequence