Re: Subsetting indexed variable?
- To: mathgroup at smc.vnet.net
- Subject: [mg36100] Re: Subsetting indexed variable?
- From: hartmut.wolf at t-systems.com (Hartmut Wolf)
- Date: Wed, 21 Aug 2002 05:52:09 -0400 (EDT)
- References: <ajajgf$9to$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Gareth J. Russell" <gjr2008 at columbia.edu> wrote in message news:<ajajgf$9to$1 at smc.vnet.net>... > Hi, > > I cannot seem to work out how to extract a subset of information from an > indexed variable (without generating a full list/matrix first). > > For example, suppose I define: > > b[1,3] = 3 > b[2,3] = 7 > b[2,4] = 5 > > And I then want to get the list of all DEFINED values of b[2,_], i.e., the > result should be {7,5} > > Help appreciated! > > Gareth > > ========================================================== > Dr. Gareth J. Russell > Columbia University > E-mail: gjr2008 at columbia.edu > ========================================================== Gareth, you might look at all definitions of b: DownValues[b] {HoldPattern[b[1, 3]] :> 3, HoldPattern[b[2, 3]] :> 7, HoldPattern[b[2, 4]] :> 5} or only at those matching b[2,_], e.g. Cases[DownValues[b], b[2, _], {3}] {7, 5} Explanation: the pattern b[2,_] matches at level 3 giving {b[2,3], b[2,4]} which is evaluated in turn giving {7, 5}. Doing so is bit dangerous however, since b[2,_] also might show up at the rhs (at this level). If you want to get at the unevaluated definitions, then you may use Cases[DownValues[b], d : (Verbatim[HoldPattern][b[2, _]] :> s_) :> d] {HoldPattern[b[2, 3]] :> 7, HoldPattern[b[2, 4]] :> 5} or if you only like to see the rhs (know what you are doing), then Cases[DownValues[b], (Verbatim[HoldPattern][b[2, _]] :> s_) :> HoldForm[s]] {7, 5} If you drop the HoldForm for s, then again you'll get the results evaluated (which doesn't matter in this example, but try with b[2, 0] := Print["killroy"]). Regard that matches are now at level 1. -- Hartmut