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
- Follow-Ups:
- Re: nested list creation with index skipping
- From: "Chris Chiasson" <chris@chiasson.name>
- Re: nested list creation with index skipping