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 two matrices A and B of identical dimensions. What's the simplest (shortest) way to find the indices of differing elements?

share|improve this question
related: 43938 – Kuba May 8 '14 at 17:49
up vote 8 down vote accepted

I've failed to find a duplicate so:

SparseArray[A - B]["NonzeroPositions"]

Position[A - B, x_ /; x != 0., {2}]

I hope those are numeric matrices, if not then you have to watch out on the Condition in the second solution.

share|improve this answer

Your Answer


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.