Re: Element Extraction
- To: mathgroup at smc.vnet.net
- Subject: [mg19281] Re: [mg19240] Element Extraction
- From: BobHanlon at aol.com
- Date: Thu, 12 Aug 1999 01:24:30 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Eugene,
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}};
target = {0, 0, 1};
hammingDistance[x_?VectorQ, y_?VectorQ] /; Length[x] == Length[y] :=
Plus @@ MapThread[If[#1 == #2, 0, 1] &, {x, y}];
Select[someList, hammingDistance[target, #] < 2 &]
{{0, 0, 0}, {0, 0, 1}, {0, 0, 2}, {0, 1, 1}, {0, 2, 1},
{1, 0, 1}, {2, 0, 1}}
Bob Hanlon
In a message dated 8/11/99 1:56:25 AM, kewjoi at hixnet.co.za writes:
>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?