2
$\begingroup$

This question already has an answer here:

I have a list a, which consists of some positive real numbers. I want to find the indices of elements which are larger than 10 in list a, and put the indices into another list index. The following is my code:

a = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
index = {};
For[i = 1, i <= Length[a], i++,
 If[a[[i]] > 10, index = Append[index, i]]
]
index

The output is

{6, 7, 8, 9, 10}

which is correct. Is there a better way to find the list index? For example, do not use the For loop. Thank you very much in advance.

$\endgroup$

marked as duplicate by Jason B., Michael E2, m_goldberg, ilian, Community Sep 1 '16 at 3:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 7
    $\begingroup$ Position[a, _?(# > 10 &)] . If you start at 1, if 0 just subtract 1 $\endgroup$ – ubpdqn Aug 31 '16 at 7:48
  • $\begingroup$ Thank you for your comment. :-) $\endgroup$ – Wei-Cheng Liu Aug 31 '16 at 7:54
  • $\begingroup$ @JasonB Thank you for your comment. :-) $\endgroup$ – Wei-Cheng Liu Sep 1 '16 at 3:24
2
$\begingroup$

You could use Pick and UnitStep as follows:

index = Pick[Range[Length[a]], UnitStep[10 - a], 0]
$\endgroup$

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