# How to get the indices of values in a list which are NOT an specific integer?

I got a real array A, where Dimensions[A] == {5000, 5000, 5}.

Some of the values in A are -1, others are not.

How can I use Position[] to find the indices of those values which are not -1?

• It is easy to Position[A,-1] all the indices which are -1. How to find all other indices ? – LCFactorization Nov 24 '13 at 7:41

A = Table[If[Round@# == -1, -1, #] &[RandomReal[{-2, 0}]], {5}, {5}]

{{-1, -1.85034, -0.063618, -1.97166, -1.98232},
{-1, -0.407057, -1.72254, -1.5231, -1.99315},
{-1, -1, -1, -1.98979, -1},
{-1, -1, -0.134094, -1.92523, -1.90799},
{-0.304344, -1, -1, -1.91487, -1.66092}}

SparseArray[A, Automatic, -1]["NonzeroPositions"]

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

• Error message of an example: "Part specification \!(\"NonzeroPositions\") is neither a \ machine-sized integer nor a list of machine-sized integers" – LCFactorization Nov 24 '13 at 8:14
• "NonzeroPositions" is a function argument, not a part. Did you use double brackets instead of single? – Ray Koopman Nov 24 '13 at 8:36
• It works! thanks – LCFactorization Nov 24 '13 at 8:39
• That's the one to use. +1 – Mr.Wizard Nov 24 '13 at 11:17

Position[A, Except[-1], {-1}, Heads -> False]

• It does not work for the code "Position[{1, 2, -1, 3}, Except[-1], {2}, Heads -> False]" – LCFactorization Nov 24 '13 at 8:35
• I assumed A was two dimensional, like you said in the question. Fortunately it's easy to fix; see edited answer. – Aky Nov 24 '13 at 8:42
• I would assume A to be any dimensional array /list. This should also be an answer. How can I accept both them? – LCFactorization Nov 24 '13 at 8:45
• @LCFactorization when you say the word Matrix in your question I got a real matrix A, it implies 2D matrix. A list such as {1,2,3} is not a matrix. Matrix is rectangular. Always. en.wikipedia.org/wiki/Matrix_%28mathematics%29 – Nasser Nov 24 '13 at 8:47
• I agree. thanks for pointing out this. – LCFactorization Nov 24 '13 at 8:48

Below the code will be OK . thanks

 Position[Boole[(# != -1) & /@ A], 1];

• The above does not work. Try A = Table[If[Round@# == -1, -1, #] &[RandomReal[{-2, 0}]], {5}, {5}]; Position[Boole[(# != -1) & /@ A], 1] – Nasser Nov 24 '13 at 8:50