1
$\begingroup$

I have a tuple mytup. Assume two arbitrary elements from the tuples are {a1, a2, a3, a4, a5, a6, a7, a8, a9} and {b1, b2, b3, b4, b5, b6, b7, b8, b9}.

Now I want to delete the element that satisies the condition:

a1 = b1
a2 = -b2
a3 = b3
a4 = b4
a5 = -b5
a6 = b6
a7 = b7 
a8 = -b8
a9 = -b9

and the deleted element should have a8 = -1 and a9 = 1. I tried using Gather and First to select the first element from each group but this does not work as the element with a8 = -1 and a9 = 1 is not always placed first in the group.
Is there a good way to do this?

UPDATE: from my tuple below, all elements have a8 = 1 and a9 = -1 or a8 = -1 and a9 = 1. However, the bigger data may include pairs with a8, and a9 are not plus or minus 1 and no element from this pair is deleted. For example, both {0, -1, -1, 1, 0, -1, d, 2, 3}, {0, 1, -1, 1, 0, -1, d, -2, -3} are kept.

  mytup = {{0, -1, -1, 1, 0, -1, d, 0, 1}, {0, -1, -1, 1, 0, -1, d, 1, 
    0}, {0, -1, -1, 1, 0, -1, d, 1, -1}, {0, -1, -1, 1, 0, -1, d, -1, 
    1}, {0, -1, -1, 1, 0, 0, d, 0, 1}, {0, -1, -1, 1, 0, 0, d, 1, 
    0}, {0, -1, -1, 1, 0, 0, d, -1, 1}, {0, -1, 0, 1, 0, -1, d, 0, 
    1}, {0, -1, 0, 1, 0, -1, d, 1, 0}, {0, 0, -1, 1, -1, -1, d, 0, 
    1}, {0, 0, -1, 1, -1, -1, d, -1, 1}, {0, 0, -1, 1, -1, 0, d, 0, 
    1}, {0, 0, -1, 1, -1, 0, d, -1, 1}, {0, 0, -1, 1, 0, -1, d, 0, 
    1}, {0, 0, -1, 1, 0, -1, d, 1, 0}, {0, 0, -1, 1, 0, -1, d, 
    1, -1}, {0, 0, -1, 1, 0, -1, d, -1, 1}, {0, 0, -1, 1, 0, 0, d, 0, 
    1}, {0, 0, -1, 1, 0, 0, d, 1, 0}, {0, 0, -1, 1, 0, 0, d, 
    1, -1}, {0, 0, -1, 1, 0, 0, d, -1, 1}, {0, 0, -1, 1, 1, -1, d, 0, 
    1}, {0, 0, -1, 1, 1, -1, d, 1, 0}, {0, 0, -1, 1, 1, -1, d, 
    1, -1}, {0, 0, -1, 1, 1, 0, d, 0, 1}, {0, 0, -1, 1, 1, 0, d, 1, 
    0}, {0, 0, -1, 1, 1, 0, d, 1, -1}, {0, 1, -1, 1, 0, -1, d, 
    1, -1}, {0, 1, -1, 1, 0, -1, d, -1, 1}, {0, 1, -1, 1, 0, 0, d, 1, 
    0}, {0, 1, -1, 1, 0, 0, d, 1, -1}, {0, 1, 0, 1, 0, -1, d, 0, 
    1}, {0, 1, 0, 1, 0, -1, d, 1, 0}, {1, -1, -1, 1, 0, -1, d, 0, 
    1}, {1, -1, -1, 1, 0, -1, d, 1, 0}, {1, -1, -1, 1, 0, 0, d, 0, 
    1}, {1, -1, -1, 1, 0, 0, d, 1, 0}, {1, -1, -1, 1, 0, 0, d, -1, 
    1}, {1, -1, 0, 1, 0, -1, d, 1, -1}, {1, -1, 0, 1, 0, -1, d, -1, 
    1}, {1, 0, -1, 1, 0, 0, d, 0, 1}, {1, 0, -1, 1, 0, 0, d, 1, 
    0}, {1, 0, -1, 1, 0, 0, d, 1, -1}, {1, 0, -1, 1, 0, 0, d, -1, 
    1}, {1, 0, -1, 1, 1, -1, d, 0, 1}, {1, 0, -1, 1, 1, -1, d, 1, 
    0}, {1, 0, -1, 1, 1, 0, d, 0, 1}, {1, 0, -1, 1, 1, 0, d, 1, 
    0}, {1, 0, -1, 1, 1, 0, d, 1, -1}, {1, 0, -1, 1, 1, 0, d, -1, 
    1}, {1, 0, 0, 1, 1, -1, d, 1, 0}, {1, 0, 0, 1, 1, -1, d, 1, -1}};
gathered = 
  Gather[mytup, ((#1[[1]] == #2[[1]]) && (#1[[2]] == -#2[[2]]) && \
(#1[[3 ;; 4]] == #2[[3 ;; 
           4]]) && (#1[[5]] == -#2[[5]]) && (#1[[6]] == #2[[6]]) && \
(#1[[7]] == #2[[7]]) && (#1[[8]] == -#2[[8]]) && (#1[[9]] == \
-#2[[9]])) &];
result = Map[First]@gathered
$\endgroup$
2
  • $\begingroup$ Do you mean you want to delete both elements {a1, a2, ...} and {b1, b2, ...} if they're related in that way? Or just one of them? $\endgroup$
    – thorimur
    Apr 9 at 5:30
  • $\begingroup$ @thorimur just one of them but the one with a8 = -1 and a9 = 1. I just added some update. $\endgroup$
    – emnha
    Apr 9 at 5:32
4
$\begingroup$

I think one way to do this is:

Select[mytup,
  !(#[[8]] == -1
    && #[[9]] == 1
    && MemberQ[mytup, # * {1, -1, 1, 1, -1, 1, 1, -1, -1}])
   &]

This keeps exactly the ones that fail the criterion, i.e. deletes the ones that do satisfy having their 8th part be -1, their 9th part be 1, and such that the list contains some member equal to {1, -1, 1, 1, -1, 1, 1, -1, -1} times the considered element.

Let me know if this works for you!

$\endgroup$
2
  • $\begingroup$ Thanks, it works. The MemberQ is nice. Is that * the multiplication? I think it should be but just for sure. $\endgroup$
    – emnha
    Apr 9 at 5:52
  • 1
    $\begingroup$ Yes, it is! When you multiply lists of equal length, Mathematica automatically threads the multiplication over the lists componentwise. $\endgroup$
    – thorimur
    Apr 9 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.