2
$\begingroup$

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$
3
  • 7
    $\begingroup$ Position[a, _?(# > 10 &)] . If you start at 1, if 0 just subtract 1 $\endgroup$ Commented Aug 31, 2016 at 7:48
  • $\begingroup$ Thank you for your comment. :-) $\endgroup$ Commented Aug 31, 2016 at 7:54
  • $\begingroup$ @JasonB Thank you for your comment. :-) $\endgroup$ Commented Sep 1, 2016 at 3:24

1 Answer 1

2
$\begingroup$

You could use Pick and UnitStep as follows:

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.