Re: transform Matrix and List elements toreplacement

*To*: mathgroup at smc.vnet.net*Subject*: [mg113979] Re: transform Matrix and List elements toreplacement*From*: Oliver Ruebenkoenig <ruebenko at wolfram.com>*Date*: Fri, 19 Nov 2010 06:59:47 -0500 (EST)

On Fri, 19 Nov 2010, leigh pascoe wrote: > I have a matrix that looks like this: > matrix= > {{1, 110, 120, 22, 64, 26, 24, 36, 390, 360, 1152}, > {4, 21, 16, 30, 47, 23, 124, 127, 397, 367, 1152}, > {2, 100, 103, 44, 71, 26, 81, 60, 302, 365, 1152}, > {3, 110, 165, 13, 55, 37, 27, 23, 363, 359, 1152}} > > I would like to sort it by the first element, delete the first and the > last 3 columns and transform the remaining values to Rules to give: > > {{a -> 110, b -> 120, c -> 22, d -> 64, e -> 26, f -> 24, g -> 36}, > {a -> 100, b -> 103, c -> 44, d -> 71, e -> 26, f -> 81, g -> 60}, > {a -> 110, b -> 165, c -> 13, d -> 55, e -> 37, f -> 27, g -> 23}, > {a -> 21, b -> 16, c -> 30, d -> 47, e -> 23, f -> 124, g -> 127}} > > The idea is to then evaluate a formula using these sets of substitutions > for a,b,..g. The actual matrix is of course larger than this one. > > There is no problem to sort and take the relevant parts > > sorted=Sort[matrix, #2[[1]] >= #1[[1]] &] > data=sorted[[1;;4,2;;8]] > > However I can't find a way to transform the resulting List elements to > replacement rules. > > I have tried creating a matrix with just the LHS of the rules: > > rules= Table["{a->,b->,c->,d->,e->,f->,g->}", {4}]; > > and then to StringJoin the elements of matrix and rules. However I can't > get this to work. > > I also tried generating the elements of the replacement matrix in a Perl > program and importing it into Mathematica. This seemed to give the > correct matrix, however it wouldn't actually work for the substitutions. > It took me some time to realise that the matrix was in fact: > > data//FullForm > > List[List["a->110", "b->120", "c->22", "d->64", "e->26", "f->24", > "g->36"], > List["a->0", "b->0", "c->0", "d->0", "e->2", "f->33", "g->63"], > List["a->0", "b->0", "c->2", "d->12", "e->5", "f->77", "g->86"], > List["a->113", "b->110", "c->27", "d->62", "e->18", "f->34", "g->39"]] > > i.e. there are unwanted parentheses (that are not evident when FullForm > is not specified). Can anyone help me to get the form that I want? Thanks. > > LP > > > > > > > I hope this gets you started MapThread[ Rule, {ToExpression[CharacterRange["a", "g"]], #}] & /@ SortBy[matrix, First][[All, 2 ;; -4]] Hth, Oliver