MathGroup Archive 2007

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

Search the Archive

Re: nested list creation with index skipping

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73968] Re: [mg73934] nested list creation with index skipping
  • From: "Chris Chiasson" <chris at chiasson.name>
  • Date: Sat, 3 Mar 2007 23:50:35 -0500 (EST)
  • References: <200703030609.BAA03277@smc.vnet.net>

What do you want for unspecified elements? If you want nothing, then
you won't end up with a regular array (and SparseArray seems to
require this - which is probably why individual entries in sparse
array can't have the head List). You could use Null for unspecified
elements.

On 3/3/07, J=E1nos <janos.lobb at yale.edu> wrote:
> 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
>
>
>
>


--
http://chris.chiasson.name/


  • Prev by Date: Re: Number of digits
  • Next by Date: Re: GenerateConditions->False gives fine result!
  • Previous by thread: nested list creation with index skipping
  • Next by thread: Re: nested list creation with index skipping