Re: comparing two lists
- To: mathgroup at smc.vnet.net
- Subject: [mg54321] Re: [mg54284] comparing two lists
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 17 Feb 2005 10:30:41 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Curt, Here is one solution. You will probably get better. rowPositions[mat_?MatrixQ, vec_?VectorQ] /; First[Dimensions[mat]] == Length[vec] := Module[{row, element}, Table[row = Part[mat, i]; element = Part[vec, i]; If[Part[row, j] > element, Part[Position[row, Part[row, j]], 1, 1], Unevaluated[Sequence[]]], {i, 1, Length[vec]}, {j, 1, Last[Dimensions[mat]]}]] vec = Table[Random[], {2}] {0.688775, 0.554987} mat = Table[Table[Random[], {4}], {2}] {{0.768502, 0.993643, 0.474089, 0.328082}, {0.0175592, 0.671364, 0.461452, 0.37211}} rowPositions[mat, vec] {{1, 2}, {2}} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ From: Curt Fischer [mailto:tentrillion at gmail.NOSPAM.com] To: mathgroup at smc.vnet.net 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.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