|
[Date Index]
[Thread Index]
[Author Index]
FW: comparing two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg54363] FW: [mg54284] comparing two lists
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Sat, 19 Feb 2005 02:32:10 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
-----Original Message-----
From: Wolf, Hartmut
To: mathgroup at smc.vnet.net
Subject: [mg54363] Re: [mg54284] comparing two lists
>-----Original Message-----
>From: Curt Fischer [mailto:tentrillion at gmail.NOSPAM.com]
To: mathgroup at smc.vnet.net
>Sent: Wednesday, February 16, 2005 8:36 PM
>To: mathgroup at smc.vnet.net
>Subject: [mg54363] [mg54284] comparing two lists
>
>Dear Group:
>
>I want to write a function that accepts a n-vector and an n x
>m matrix.
>It should return a list of positions in the matrix where
>mat[[i,j]] > vec[[i]]. For example,
>
>In[287]:=
>vec=Table[Random[],{2}]
>
>Out[287]=
>{0.482259,0.314393}
>
>In[288]:=
>mat=Table[Table[Random[],{4}],{2}]
>
>Out[288]=
>{{0.183706,0.758693,0.462242,0.170041},{0.457054,0.349658,0.805
>455,0.127763}}
>
>I would like myFunc[] to return {{2},{1,2,3}}.
>
>How could I write this as a pure function? Ideally I would
>like to be able to Map or Apply my function to the list {mat,
>vec} and get my result.
>
>Something like
>
>Position[#1,_?(#>#2&)]&@@{mat,vec}
>
>is doomed to fail because the PatternTest required in
>Position[] messes up the slotting of the other arguments.
>
>Ideas? How do you nest pure functions?
>
>--
>Curt Fischer
>
>
Curt,
you can easily avoid the problems of entangled function definitions by
resorting to Condition and named pattern:
In[3]:=
MapThread[Flatten@Position[#1, x_ /; x > #2] &, {mat, vec}]
Out[3]=
{{2}, {1, 2, 3}}
For your question of nesting: at least one function definition must be
made by supplying explicit argument names to Function. You can do that
one way or the other:
In[4]:=
MapThread[Function[{m, v}, Flatten[Position[m, _?(# > v &)]]], {mat,
vec}]
Out[4]=
{{2}, {1, 2, 3}}
or
In[5]:=
MapThread[Flatten[Position[#1, _?(Function[x, x > #2])]] &, {mat, vec}]
Out[5]=
{{2}, {1, 2, 3}}
The parentheses around Function[x,..] are neccessary because ?
(PatternTest) has such a strong precedence.
To your original problem, you might consider:
In[6]:=
Position[mat - vec, _?Positive, {2}]
Out[6]=
{{1, 2}, {2, 1}, {2, 2}, {2, 3}}
which is not quite of the form specified, but perhaps better.
You might convert one form to the other, e.g
In[7]:=
Split[%, First[#1] === First[#2] &][[All, All, 2]]
Out[7]=
{{2}, {1, 2, 3}}
In[8]:=
Flatten[MapIndexed[Outer[List, #2, #1] &, %], 2]
Out[8]=
{{1, 2}, {2, 1}, {2, 2}, {2, 3}}
--
Hartmut Wolf
Prev by Date:
Re: Controlling program flow across cells
Next by Date:
Re: Re: Re: Bug Report - Two numerical values for a same variable
Previous by thread:
Re: comparing two lists
Next by thread:
Re: comparing two lists
|