9
$\begingroup$

I have a table, testdata, with numeric and non-numeric data.

testData = {{1, 2, 3, no, 5}, {5, 6, 7, 8, 9.1}, {1, y, y, n, n}};

I can locate the positions of numeric data easily

goodloc = Position[testData, _?NumericQ]

(* {{1, 1}, {1, 2}, {1, 3}, {1, 5}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2,   5}, {3, 1}} *)

I would like an equally simple means to locate non-numeric variables

I have been able to get this result by using this code

 allPos = Flatten[Table[{i, j}, {i, 3}, {j, 5}], 1];
 badloc = Complement[allPos, goodloc]

(* {{1, 4}, {3, 2}, {3, 3}, {3, 4}, {3, 5}} *)

Is there a single expression that will do this?

$\endgroup$
  • 1
    $\begingroup$ your oneliner might look like Complement[Flatten[MapIndexed[#2 &, testData, {2}], 1], Position[testData, _?NumericQ]] $\endgroup$ – garej Mar 26 '16 at 21:31
14
$\begingroup$

You can simply do:

Position[testData, _?(Not@*NumericQ), {2}, Heads -> False]

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

Notice the use of level specification so that you only look inside the sublists and the option Head -> False prevents you from including the position of Heads, since they are non-numeric. An alternative is to use Except as Kglr mentioned in the comments:

Position[testData, Except[_?NumericQ], {2}, Heads -> False]
$\endgroup$
  • 6
    $\begingroup$ ... or Position[testData, Except[_?NumericQ], {2}, Heads -> False] (+1) $\endgroup$ – kglr Mar 26 '16 at 19:21
  • $\begingroup$ @kglr. Thanks, that's a good alternative. $\endgroup$ – RunnyKine Mar 26 '16 at 19:25
4
$\begingroup$
Position[Map[NumericQ, testData, {-1}], False]

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

$\endgroup$
  • 1
    $\begingroup$ or Position[Map[NumericQ, testData, {2}], False] $\endgroup$ – garej Mar 27 '16 at 6:52
  • $\begingroup$ This is just what I was looking for. Thank you $\endgroup$ – martin keane Mar 27 '16 at 23:18

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.