10
$\begingroup$

This question has been solved. Thanks for all your help!

I have data that looks like this.

{{{1,3}},{{2,3},{5,2},{2,3},{4,1}},{{4,3},{1,1},{3,2},{5,4},{7,6}},{{2,6},{3,3}}}

These represent the position of objects and their trajectories at different times. So, that the first object {{1,3}} only stayed at that one position with coordinates {1,3}. But, the second object {{2,3},{5,2},{2,3},{4,1}} was in 4 positions.

I would like to delete only the objects that moved more than 2 times, and that have the same position at times 1 and 3. So, an example would be:

{{2,3},{5,2},{2,3},{4,1}}

And I would like to keep all the other data.

I would appreciate if some could help with figuring this out.

Thanks!

$\endgroup$
  • 1
    $\begingroup$ @Andres This is actually not multiple conditions: If your objects have the same position at times 1 and 3, it is clear that they moved >2 times :) $\endgroup$ – yarchik Mar 4 '16 at 9:07
  • $\begingroup$ I noticed that you put "the question has now been solved" at the top of your question. This is not how you are supposed to do it, you are supposed to "accept" an answer by using the check mark next to an answer. This will signal to everybody that you consider the problem solved. $\endgroup$ – C. E. Mar 11 '16 at 22:56
13
$\begingroup$

This should do it:

DeleteCases[list, {x_, _, x_, ___}]

or equivalently

list /. {x_, _, x_, ___} -> Nothing
$\endgroup$
6
$\begingroup$

@Runnykine pointed out corectly that I only address 1 criterion.

Corrected Answer

DeleteCases[d, _?(Length@# > 2 && #[[1]] == #[[3]] &)]
Pick[d, Or[Length@# < 3, #[[1]] != #[[3]]] & /@ d]
Select[d, Or[Length@# < 3, #[[1]] != #[[3]]] &]

Original Answer

Just some variants:

Pick[d, Length /@ d, _?(# < 3 &)]
Select[d, Length@# < 3 &]
Cases[d, _?(Length@# < 3 &)]

where d is list

$\endgroup$
  • $\begingroup$ This does not address the second criterion - that the positions at times 1 and 3 be the same for deletion. $\endgroup$ – RunnyKine Mar 4 '16 at 8:26
  • $\begingroup$ @RunnyKine I missed that. I will address when I am near computer. Thanks $\endgroup$ – ubpdqn Mar 4 '16 at 8:28
  • $\begingroup$ I think you meant to put that in your answer and not the question. $\endgroup$ – RunnyKine Mar 4 '16 at 8:47
  • $\begingroup$ @RunnyKine shouldn't do MSE when you are sick...making all sorts of blunders today $\endgroup$ – ubpdqn Mar 4 '16 at 8:49
  • $\begingroup$ Yeah, I feel you. +1 for your efforts :) $\endgroup$ – RunnyKine Mar 4 '16 at 8:51
4
$\begingroup$

This works as well

Select[
 list,
 If[Length[#] > 2,
   If[#[[1]] == #[[3]],
    False,
    True
    ],
   True
   ] &
 ]

And i think it's easier to understand how it works (well, at least for me).

$\endgroup$
  • 2
    $\begingroup$ If[whatever, False, True] (or the reverse) is usually frowned upon by serious programmers (those that don' t laugh often).They usually suggest things like Select[list, Length[#] < 3 || #[[1]] != #[[3]] &] instead $\endgroup$ – Dr. belisarius Mar 4 '16 at 2:36
  • $\begingroup$ Won't argue with that. However I wanted to explicitly specify that it is greater than 2. $\endgroup$ – Andrew S. Mar 4 '16 at 2:42

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.