3
$\begingroup$

I've a list where a I want to pick up my first element which matches my criteria. This is my work around:

While[n < Length[listRedPoint],
    If[Sign[listRedPoint[[n]].Take[w, 2] + w[[3]]] != -1, 
    wrongData = {listRedPoint[[n]], -1};
    Break[]
    ];
    n++
];

where listRedPoint is a list of m pairs {x_n,y_n}, w is a 3-dimension vector wrongData is the data picked up. In general is there any way to optimize this search-algorithm without use while loop? (Select[]?)

$\endgroup$
  • $\begingroup$ It seems Select can do what you want, have you tried to use it? $\endgroup$ – Kuba Jun 13 '16 at 8:58
  • $\begingroup$ I've tried Select[listRedPoint,Sign[#.Take[w, 2] + w[[3]]] != -1,1] but i got an error.... $\endgroup$ – user3371854 Jun 13 '16 at 9:22
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jun 13 '16 at 10:33
4
$\begingroup$

SelectFirst instead of While-Break.

SelectFirst[listRedPoint,
 Sign[#.Take[w, 2] + Last[w]] > 0 &]
$\endgroup$
  • $\begingroup$ i suggest also SelectFirst[listRedPoint, Sign[#.Take[w, 2] + Last[w]] > 0 &,1] for users with version <10.... $\endgroup$ – user3371854 Jun 14 '16 at 19:16

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.