Re: Element Extraction
- To: mathgroup at smc.vnet.net
- Subject: [mg19274] Re: [mg19240] Element Extraction
- From: "Andrzej Kozlowski" <andrzej at tuins.ac.jp>
- Date: Thu, 12 Aug 1999 01:24:26 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Here is one attempt. However, I have no idea if it is efficient. We first define a function dist which is zero if two elements are equal and 1 otherwise and give it the attribute Listable. In[3]:= dist[a_, b_] := If[a === b, 0, 1] In[4]:= SetAttributes[dist, Listable] Now we can do our extraction: In[5]:= Select[someList, Apply[Plus, dist[target, #]] <= 1 &] Out[5]= {{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 1, 1}, {0, 2, 1}, {1, 0, 1}, {2, 0, 1}} This works for lists of lists of arbitrary length. the elements need not be numbers and you can extract elements which differ from the target in any given number of places. -- Andrzej Kozlowski Toyama International University JAPAN http://sigma.tuins.ac.jp http://eri2.tuins.ac.jp ---------- >From: kewjoi at hixnet.co.za (Kew Joinery) To: mathgroup at smc.vnet.net >To: mathgroup at smc.vnet.net >Subject: [mg19274] [mg19240] Element Extraction >Date: Wed, Aug 11, 1999, 8:06 AM > > Hello everyone, > I have a problem extracting list of list. > Given some list, consisting of elements (not all of them distinct) with > the same length and some target list of the same length, for example: > In[26]:= > someList={{0,0,0},{0,0,1},{0,0,2},{0,1,0},{0,1,1},{0,1,2},{0,2,0},{0,2,1},{0, > > > > 2,2},{1,0,0},{1,0,1},{1,0,2},{1,1,0},{1,1,1},{1,1,2},{1,2,0},{1,2,1},{1, > > > > 2,2},{2,0,0},{2,0,1},{2,0,2},{2,1,0},{2,1,1},{2,1,2},{2,2,0},{2,2,1},{2, > > 2,2}}; > > In[27]:= > target={0,0,1}; > > Hamming distance between two bit strings means the number of bit > positions in which they differ. For example consecutive elements of the > Gray code list have Hamming distance = 1. > I need to extract these cases(elements of someList) which differ from > target in one coordinate (Hamming distance = 1) or less (the > element===target itself//Hamming distance=0), so the answer should be: > > In[33]:= > answer={{0,0,0},{0,0,1},{0,0,2},{0,1,1},{0,2,1},{1,0,1},{2,0,1}}; > > How could I achieve the extraction efficiently? > Thank you in advance for any suggestions. > Eugene > > > > > >