MathGroup Archive 2005

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

Search the Archive

Re: Add terms surrounded by zero together in matrix


I suggest you use Combinatorica
and proceed roughly like this
(I didn't deal with side effects):


In[1]:=<<DiscreteMath`Combinatorica`

In[2]:=m={{0,0,0,1,0},{0,0,1,2,0},{0,0,0,2,1},{1,3,0,0,0},
{0,0,0,0,0},{0,0,0,0,0},{0,0,1,1,0},
{5,0,3,0,0},{0,0,0,0,0},{0,0,0,3,1}};

im=Length[m];
jm=Length[m[[1]]];

select non nul vertices :

vtx=Outer[List,Range[im],Range[jm]]//Flatten[#,1]&//
    Select[#,m[[#[[1]],#[[2]]]] != 0&]&;

f = function to tell if 2 vertices are connected

f[{i1_,j1_},{i1_,j2_/;j2 <= jm}]/;j2 == j1+1:=
    m[[i1,j1]] != 0 && m[[i1,j1+1]] != 0;

f[{i1_,j1_},{i2_/;i2 <= im,j1_}]/;i2 == i1+1:=
    m[[i1,j1]] != 0 && m[[i1+1,j1]] != 0;

[here : more to specify for f ... ]

In[8]:=gr=MakeGraph[vtx,f,Type -> Undirected];

In[9]:=ConnectedComponents[gr]

Out[9]={{1,2,3,4,5},{6,7},{8,9,11},{10},{12,13}}

etc. etc.


hth

Valeri Astanoff


  • Prev by Date: Re: NIntegrate::inum
  • Next by Date: Re: silly questions?
  • Previous by thread: Re: Add terms surrounded by zero together in matrix
  • Next by thread: Re: Add terms surrounded by zero together in matrix