Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
1  
your oneliner might look like Complement[Flatten[MapIndexed[#2 &, testData, {2}], 1], Position[testData, _?NumericQ]] – garej Mar 26 at 21:31

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]
share|improve this answer
6  
... or Position[testData, Except[_?NumericQ], {2}, Heads -> False] (+1) – kglr Mar 26 at 19:21
    
@kglr. Thanks, that's a good alternative. – RunnyKine Mar 26 at 19:25
Position[Map[NumericQ, testData, {-1}], False]

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

share|improve this answer
1  
or Position[Map[NumericQ, testData, {2}], False] – garej Mar 27 at 6:52
    
This is just what I was looking for. Thank you – martin keane Mar 27 at 23:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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