TransWikia.com

Conditional matches in sets of 3-tuples

Mathematica Asked by thinkingdude on February 5, 2021

Suppose that the data is a collection of 3-tuples:

   data={{{0, 1, 1}, {0, 1, 1}, {0, 0, 1}}, {{1, 0, 0}, {0, 1, 0}, {1, 1, 
   1}}, {{1, 0, 1}, {0, 1, 0}, {0, 0, 0}}, {{1, 1, 1}, {1, 1, 1}, {1, 
   0, 0}}, {{1, 0, 1}, {1, 0, 0}, {0, 0, 0}}, {{1, 1, 1}, {1, 1, 
   1}, {0, 0, 0}}, {{0, 1, 0}, {0, 0, 0}, {0, 1, 0}}, {{0, 1, 1}, {0, 
   0, 1}, {0, 0, 1}}, {{0, 1, 1}, {1, 0, 1}, {1, 1, 1}}, {{0, 1, 
   1}, {0, 1, 1}, {1, 0, 0}}, {{1, 1, 0}, {0, 1, 1}, {0, 0, 0}}, {{0, 
   0, 0}, {1, 1, 1}, {1, 1, 0}}, {{1, 1, 1}, {1, 1, 1}, {1, 0, 
   0}}, {{0, 0, 0}, {0, 1, 0}, {0, 1, 0}}, {{0, 1, 0}, {0, 1, 0}, {1, 
   1, 0}}, {{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0, 1, 0}, {1, 0, 
   1}, {0, 1, 1}}, {{0, 0, 1}, {0, 1, 0}, {0, 1, 1}}, {{1, 1, 1}, {1, 
   1, 0}, {1, 1, 1}}, {{0, 0, 1}, {0, 0, 1}, {0, 0, 1}}, {{1, 0, 
   0}, {0, 0, 1}, {1, 0, 0}}, {{1, 0, 1}, {1, 1, 0}, {1, 1, 0}}, {{1, 
   0, 1}, {1, 1, 1}, {1, 0, 1}}, {{1, 1, 1}, {1, 0, 0}, {0, 1, 
   0}}, {{1, 0, 1}, {0, 0, 0}, {1, 0, 1}}, {{0, 0, 0}, {0, 0, 0}, {0, 
   1, 1}}, {{0, 1, 0}, {0, 1, 0}, {1, 0, 1}}, {{1, 1, 1}, {1, 0, 
   0}, {1, 0, 0}}, {{1, 0, 0}, {0, 0, 0}, {0, 1, 1}}, {{0, 0, 0}, {1, 
   0, 1}, {1, 1, 0}}, {{1, 0, 1}, {0, 1, 1}, {1, 1, 1}}, {{0, 1, 
   1}, {0, 1, 1}, {1, 0, 0}}, {{0, 0, 0}, {0, 0, 1}, {0, 0, 0}}}

How to find only those set of 3 tuples in the data where the 2nd and 3rd elements of their 1st 3-tuple equals the first and the second elements of the 2nd 3-tuple and that the 2nd and the 3rd elements of the 2nd 3-tuple equals the 1st and 2nd element of the 3rd 3-tuple.

3 Answers

Select[Most[Rest /@ #] == Rest[Most /@ #] &] @ data
{{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}},
 {{0, 1, 0}, {1, 0, 1}, {0, 1, 1}}, 
 {{1, 0, 1}, {0, 1, 1}, {1, 1, 1}}}

Answered by kglr on February 5, 2021

If I understood you correctly, something like this would work

Cases[data,{{_,a_,b_},{a_,b_,c_},{b_,c_,_}}]

(* {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, 
    {{0, 1, 0}, {1, 0, 1}, {0, 1, 1}}, 
    {{1, 0, 1}, {0, 1, 1}, {1, 1, 1}}} *)

Answered by Hausdorff on February 5, 2021

Pick[
 data,
 MapThread[SameQ, {
   data[[All, ;; -2, {2, 3}]],
   data[[All, 2 ;;, {1, 2}]]
   }]
 ]

(* Out: {{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}, {{0, 1, 0}, {1, 0, 1}, {0, 1, 
   1}}, {{1, 0, 1}, {0, 1, 1}, {1, 1, 1}}} *)

Answered by C. E. on February 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP