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

MathGroup Archive 2005

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

Search the Archive

Re: Converting a mapping into a well-defined function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg56541] Re: Converting a mapping into a well-defined function
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Thu, 28 Apr 2005 02:40:42 -0400 (EDT)
  • Organization: University of Washington
  • References: <d4msni$257$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

"Gilmar" <gilmar.rodriguez at nwfwmd.state.fl.us> wrote in message 
news:d4msni$257$1 at smc.vnet.net...
> Dear Mathematica User Friends:
>
> I need to build a module that takes a non-empty set A
> (whose elements are 2-tuples), and appends (or not) those 2-tuples
> to a set B  according to the following criteria:
>
> (1.) if the abscissa of a 2-tuple in A is unique (among the abscissas
> of all the other 2-tuples in A), then the module appends that 2-tuple
> to the set B.
>
> (2.) if there are n 2-tuples in A that share the same abscissa, then
> the module appends the first of those 2-tuples to B ("first" here means
> the first of such n 2-tuple encountered, as you read A from left to
> right),but does not append the rest to B.
>
> (3.) the module does not attempt to sort the set B, after the appending
> process is completed.
>
> Example:
>
> If A = {{a,5},{b,27},{a,14},{c,4},{e,94},{b,6},{d,9},{e,4}}
>
> then
>
> B=Module[A]={{a,5},{b,27},{c,4},{d,9},{e,94}}.
>

One possibility is the following:

f[x_]:=Module[{tmp},
 tmp = Transpose[{x[[All,1]],Range[Length[x]],x[[All,2]]}];
 Split[Sort[tmp],#1[[1]]===#2[[1]]&][[All,1,{1,3}]]
 ]

For your example,

A={{a,5},{b,27},{a,14},{c,4},{e,94},{b,6},{d,9},{e,4}};

f[A]
{{a, 5}, {b, 27}, {c, 4}, {d, 9}, {e, 94}}

as desired.

> Another way to re-phrase the above example;
>
> if A defines a mapping:
>
> A(a)=5, A(b)=27, A(a)=14, A(c)=4, A(e)=94, A(b)=6, A(d)=9, A(e)=4
>
> them the module converts the mapping A into a well-defined function:
>
> A(a)=5, A(b)=27, A(c)=4, A(d)=9, A(e)=94.
>
> Thank you for your help!
>

If you are interested in creating such a function, then one idea follows:

g[x_] := Module[{},
 Clear[h];
 Set @@@ Reverse[{h[#1], #2}& @@@ x];
 ]

g[A]

?h
Global`h
h[a] = 5

h[b] = 27

h[c] = 4

h[d] = 9

h[e] = 94

Carl Woll 



  • Prev by Date: Re: Re: odd mathematica blindspot
  • Next by Date: Re: Converting a mapping into a well-defined function
  • Previous by thread: Re: Converting a mapping into a well-defined function
  • Next by thread: Re: Converting a mapping into a well-defined function