Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

nested list creation with index skipping

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73934] nested list creation with index skipping
  • From: János <janos.lobb at yale.edu>
  • Date: Sat, 3 Mar 2007 01:09:44 -0500 (EST)

Hi,

What is the most economical way to create a nested list where during 
the creation I have to jump over some index values?

For a simple example
n=10;
tbl=Table[Table[j,{j,1,n}],{i,1,n}]

Here I would like to skip any index where i==j.  Table does not give 

me the option Assuming[ i=!=j] or something similar.

Of course I can drop later on those elements where i==j  , but that 

is additional processing and not economical, like:

Table[Select[tbl[[i]], #1 =!= i & ], {i, 1, n}]

The best I can do as a newbie, is to have Reap-Sow cycle where I do 
NOT collect values for i==j, like:

lst = First[Last[Reap[i = 1; While[i <= n,
        rj = First[Last[Reap[j = 1; While[j <= n,
               If[j =!= i, Sow[j], Null]; j++; ]; ]]];
         Sow[rj]; i++; ]; ]]]

In general, I can have two lists for example:
iis={1,2,5,6,8,9}
jjs={2,3,5,7,8}
Where the values in these lists indicate what index values I want to 
take into account when I create the list.

With the Reap-Sow cycle it can be done this way:

lst = First[Last[Reap[i = 1; While[i <= n,
        rj = First[Last[Reap[j = 1; While[j <= n,
               If[j =!= i && MemberQ[jjs, j], Sow[j],
                 Null]; j++; ]; ]]]; If[MemberQ[iis, i],
          Sow[rj], Null]; i++; ]; ]]]


I guess it leads to SparseArray sooner or later, but I do not want 0 
for unspecified elements like SparseArray does.

Thanks ahead,

J=E1nos




  • Prev by Date: Re: Compile question
  • Next by Date: Re: fastest way to add up a billion numbers
  • Previous by thread: Re: Cases to Select ?
  • Next by thread: Re: nested list creation with index skipping