4

How do I determine the position of rows whose entries all satisfy some condition (e.g., all elements of the row are positive)? I've found out how to use Select to extract the correct rows, but I haven't been able to find out how to use Position to do the equivalent.

A = {{-1, 2, 3}, {4, 5, 6}, {-7, 8, 9}}
Select[A, And @@ Thread[# > 0] &]

gives

{{4, 5, 6}}

but I want to know the position of that row (i.e. {2}).

Kuba
  • 136,707
  • 13
  • 279
  • 740
user12734
  • 349
  • 1
  • 7

3 Answers3

5

Use the function VectorQ,

A = {{-1, 2, 3}, {4, 5, 6}, {-7, 8, 9}};
Position[A, x_ /; VectorQ[x, # > 0 &], {1}]

or

Position[VectorQ[#, # > 0 &] & /@ A, True, {1}]

As Mr.Wizard points out that you should use a levelspec {1} to avoid testing subexpressions.

Ukiyo-E
  • 259
  • 1
  • 4
3
aA = {{-1, 2, 3}, {4, 5, 6}, {-7, 8, 9}};

Position[And @@ Thread[# > 0] & /@ aA, True]
(* {{2}} *)

Or, using the third argument of Position to set the levelspec to 1:

Position[aA, _?(And @@ Thread[# > 0] &), 1]
(* {{2}} *)
kglr
  • 394,356
  • 18
  • 477
  • 896
1

Without #, yeah:

Position[A, {__?Positive}, {1}]
{{2}}
Kuba
  • 136,707
  • 13
  • 279
  • 740