[Date Index]
[Thread Index]
[Author Index]
Re: comparing two lists
*To*: mathgroup at smc.vnet.net
*Subject*: [mg54328] Re: [mg54284] comparing two lists
*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>
*Date*: Thu, 17 Feb 2005 10:30:46 -0500 (EST)
*References*: <200502161936.OAA19139@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
On 16 Feb 2005, at 20:36, Curt Fischer wrote:
> 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.805455,0.12
> 7763}}
>
> 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
>
>
I would say the simplest way to solve your problem is:
MyFunction1[mat_?MatrixQ, vec_?VectorQ] :=Position[mat - vec, _?(# > 0
&)]
For example with your data this gives:
In[2]:=
mat={{0.183706,0.758693,0.462242,0.170041},{0.457054,0.349658,0.805455,0
.\
127763}};
In[3]:=
vec={0.482259,0.314393};
In[4]:=
MyFunction1[mat,vec]
Out[4]=
{{1,2},{2,1},{2,2},{2,3}}
This tells you exactly the positions you wanted but not in the form you
asked for. Getting the output into that form will be the most
complicated part of the problem, so if you really do not need it I
would rather settle for the above. But of course there are lots of ways
to get exactly the answer you asked for. You could try to bring the
answer above into the form you want or you could use something like:
MyFunction2[mat_?MatrixQ, vec_?VectorQ] :=
Map[Flatten[Position[mat[[#]] -
vec[[#]], _?(# > 0 &)]] &, Range[Length[vec]]]
In[6]:=
MyFunction2[mat,vec]
Out[6]=
{{2},{1,2,3}}
and lots of other ways.
Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/andrzej/index.html
http://www.mimuw.edu.pl/~akoz/
Prev by Date:
**Re: Re: Bug Report - Two numerical values for a same variable**
Next by Date:
**Re:**
Previous by thread:
**Re: comparing two lists**
Next by thread:
**Re: comparing two lists**
| |